using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Telerik.Web.UI;

public partial class _Default : System.Web.UI.Page
{
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridHeaderItem)
        {
            foreach (GridColumn column in e.Item.OwnerTableView.RenderColumns)
            {
                if (column is GridBoundColumn && ((GridBoundColumn)column).AllowSorting ||
                    column is GridCheckBoxColumn && ((GridCheckBoxColumn)column).AllowSorting ||
                    column is GridDropDownColumn && ((GridDropDownColumn)column).AllowSorting)
                {
                    RegisterClientSortCommand((GridHeaderItem)e.Item, column);
                }
            }
        }
    }

    protected void RegisterClientSortCommand(GridHeaderItem item, GridColumn column)
    {
        LinkButton LinkButton1 = (LinkButton)item[column.UniqueName].Controls[0];

        string dataField = "";
        if (column is GridBoundColumn)
        {
            dataField = ((GridBoundColumn)column).DataField;
        }
        else if (column is GridCheckBoxColumn)
        {
            dataField = ((GridCheckBoxColumn)column).DataField;
        }
        else if (column is GridDropDownColumn)
        {
            dataField = ((GridDropDownColumn)column).DataField;
        }

        LinkButton1.OnClientClick = String.Format("onSortCommand(this, '{0}'); return false;", LinkButton1.CommandArgument);
    }

    protected void RadGrid1_ColumnCreated(object sender, GridColumnCreatedEventArgs e)
    {
        if (e.Column.IsBoundToFieldName("UnitPrice"))
        {
            ((GridBoundColumn)e.Column).DataFormatString = "{0:c}";
        }
        else if (e.Column.IsBoundToFieldName("Date"))
        {
            ((GridBoundColumn)e.Column).DataFormatString = "{0:d}";
        }
        else if (e.Column.IsBoundToFieldName("Name"))
        {
            ((GridBoundColumn)e.Column).AllowSorting = false;
        }
    }
}
