using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

public partial class RadGridHeaderContextMenuAddItems : System.Web.UI.Page
{
    protected override void OnPreRenderComplete(EventArgs e)
    {
        RadContextMenu menu = RadGrid1.HeaderContextMenu;
        RadMenuItem item = new RadMenuItem();
        item.Text = "Format";
        item.Attributes["ColumnName"] = string.Empty;
        item.Attributes["TableID"] = string.Empty;
        BuildColumnsMenu(item, RadGrid1.MasterTableView);
        menu.Items.Add(item);
        base.OnPreRenderComplete(e);
    }
    private void BuildColumnsMenu(IRadMenuItemContainer columnsParentItem, GridTableView tableView)
    {
        if (tableView.EnableHeaderContextMenu)
        {
            foreach (GridColumn gridColumn in tableView.RenderColumns)
            {
                if (gridColumn is GridGroupSplitterColumn || gridColumn is GridExpandColumn || gridColumn is GridRowIndicatorColumn || !gridColumn.Visible)
                    continue;
                PrepareItem(gridColumn, columnsParentItem);
            }
        }
    }
    private void PrepareItem(GridColumn column, IRadMenuItemContainer columnsParentItem)
    {
        if (column.DataType == typeof(int))
        {
            RadMenuItem columnItem = new RadMenuItem();
            columnItem.Attributes["ColumnName"] = column.UniqueName;
            columnItem.Attributes["TableID"] = column.Owner.UniqueID;
            columnItem.Value = string.Format("{0}", "{0:D}");
            columnItem.Text = "Format D";
            columnItem.PostBack = true;
            columnsParentItem.Items.Add(columnItem);
            columnItem = new RadMenuItem();
            columnItem.Attributes["ColumnName"] = column.UniqueName;
            columnItem.Attributes["TableID"] = column.Owner.UniqueID;
            columnItem.Value = string.Format("{0}", "{0:D8}");
            columnItem.Text = "Format D8";
            columnItem.PostBack = true;
            columnsParentItem.Items.Add(columnItem);
        }
        else if (column.DataType == typeof(DateTime))
        {
            RadMenuItem columnItem = new RadMenuItem();
            columnItem.Attributes["ColumnName"] = column.UniqueName;
            columnItem.Attributes["TableID"] = column.Owner.UniqueID;
            columnItem.Value = string.Format("{0}", "{0:MM/dd/yyyy}");
            columnItem.Text = "Format MM/dd/yyyy";
            columnItem.PostBack = true;
            columnsParentItem.Items.Add(columnItem);
            columnItem = new RadMenuItem();
            columnItem.Attributes["ColumnName"] = column.UniqueName;
            columnItem.Attributes["TableID"] = column.Owner.UniqueID;
            columnItem.Value = string.Format("{0}", "{0:yyyy/MM/dd}");
            columnItem.Text = "Format yyyy/MM/dd";
            columnItem.PostBack = true;
            columnsParentItem.Items.Add(columnItem);
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        RadGrid1.HeaderContextMenu.ItemClick += new RadMenuEventHandler(HeaderContextMenu_ItemClick);
    }

    protected void HeaderContextMenu_ItemClick(object sender, RadMenuEventArgs e)
    {
        string tableID = e.Item.Attributes["TableID"];
        string columnName = e.Item.Attributes["ColumnName"];
        string format = e.Item.Value;
        GridTableView tableView = (GridTableView)this.Page.FindControl(tableID);
        GridBoundColumn column = (GridBoundColumn)tableView.GetColumn(columnName);
        column.DataFormatString = format;
        RadGrid1.Rebind();
    }
}