This is a migrated thread and some comments may be shown as answers.

How to add custom item to context menu in rad grid

2 Answers 510 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Vaibhav
Top achievements
Rank 1
Vaibhav asked on 13 Feb 2014, 06:49 AM
Hi All,

I want to add the a custom item to context menu on radgrid header context menu like "Default settings \ Clear settings"
and on click of this event in want to fire a server side code which i have .

Could any one help me on this , how can we achieve this , with a small piece of sample code

Thanks

2 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 13 Feb 2014, 07:24 AM
Hi Vaibhav,

Please try the following code snippet to add a custom menu to the ContextMenu.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" EnableHeaderContextMenu="true". .>
    <HeaderContextMenu OnItemClick="HeaderContextMenu_ItemClick" OnClientItemClicking="contextMenuItemClicking">
    </HeaderContextMenu>
    <MasterTableView>
        <Columns>
            <telerik:GridBoundColumn UniqueName="OrderID" DataField="OrderID" HeaderText="OrderID"/>         
            <telerik:GridBoundColumn DataField="ShipCity" HeaderText="ShipCity" UniqueName="ShipCity"/>
        </Columns>
    </MasterTableView>
    <ClientSettings>
        <ClientEvents OnColumnContextMenu="columnContextMenu" />
    </ClientSettings>
</telerik:RadGrid>

JS:
<script type="text/javascript">
    var columnName;
    function columnContextMenu(sender, args) {
        columnName = args.get_gridColumn().get_uniqueName();
    }
    function contextMenuItemClicking(sender, args) {
        if (args.get_item().get_text() === "Default Settings") {
         sender.trackChanges();
         args.get_item().get_attributes().setAttribute("ColumnName", columnName);
         sender.commitChanges();
        }
    }
</script>

C#:
protected void Page_Load(object sender, EventArgs e)
{
   RadGrid1.HeaderContextMenu.PreRender += new EventHandler(HeaderContextMenu_PreRender); 
}
void HeaderContextMenu_PreRender(object sender, EventArgs e)
{
   RadMenuItem newItem = new RadMenuItem("Default Settings"); // Set the item
   newItem.Attributes["TableID"] = RadGrid1.UniqueID;
   RadGrid1.HeaderContextMenu.Items.Add(newItem);
}
protected void HeaderContextMenu_ItemClick(object sender, RadMenuEventArgs e)
{
   string tableID = e.Item.Attributes["TableID"];
   if (e.Item.Text == "Default Settings")
   {
    string columnName = e.Item.Attributes["ColumnName"]; // Get the column            
   }
}

Thanks,
Princy
0
Chaitanya
Top achievements
Rank 1
answered on 05 Mar 2014, 09:49 PM
Hi Princy,

I was able to add the custom item to the header context menu but how can I make the item to appear top in the context menu list.

Thanks in advance

Chaitanya
Tags
Grid
Asked by
Vaibhav
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Chaitanya
Top achievements
Rank 1
Share this question
or