Edit this page

Modifying the Default Context Menu

The default RadGridView context menu can be customized in the ContextMenuOpening event handler.

Removing an item from default RadGridView context menu:

In order to remove an item, you need to make a loop iterating the e.ContextMenu.Items and check if the e.ContextMenu.Items[index].Text is equal to the text of the menu item that you want to hide. If so, just set the Visibility of the menu item to Collapsed:

void radGridView1_ContextMenuOpening(object sender, Telerik.WinControls.UI.ContextMenuOpeningEventArgs e)
{
    for (int i = 0; i < e.ContextMenu.Items.Count; i++)
    {
        if (e.ContextMenu.Items[i].Text == "Conditional Formatting")
        {
            // hide the Conditional Formatting option from the header row context menu
            e.ContextMenu.Items[i].Visibility = Telerik.WinControls.ElementVisibility.Collapsed;
            // hide the separator below the CF option
            e.ContextMenu.Items[i + 1].Visibility = Telerik.WinControls.ElementVisibility.Collapsed;
        }
    }
}

Private Sub RadGridView1_ContextMenuOpening(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.ContextMenuOpeningEventArgs) Handles RadGridView1.ContextMenuOpening
    Dim i As Integer = 0
    Do While i < e.ContextMenu.Items.Count
        If e.ContextMenu.Items(i).Text = "Conditional Formatting" Then
            ' hide the Conditional Formatting option from the header row context menu
            e.ContextMenu.Items(i).Visibility = Telerik.WinControls.ElementVisibility.Collapsed
            ' hide the separator below the CF option
            e.ContextMenu.Items(i + 1).Visibility = Telerik.WinControls.ElementVisibility.Collapsed
        End If
        i += 1
    Loop
End Sub

Adding menu items to the default RadGridView context menu

In order to add custom menu items to the default context menu, you should create menu item instances in the ContextMenuOpening event handler and add them to the e.ContextMenu.Items:

void radGridView1_ContextMenuOpening1(object sender, Telerik.WinControls.UI.ContextMenuOpeningEventArgs e)
{
    RadMenuItem customMenuItem = new RadMenuItem();
    customMenuItem.Text = "Custom Data Operation";
    RadMenuSeparatorItem separator = new RadMenuSeparatorItem();
    e.ContextMenu.Items.Add(separator);
    e.ContextMenu.Items.Add(customMenuItem);
}

Private Sub RadGridView1_ContextMenuOpening1(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.ContextMenuOpeningEventArgs) Handles RadGridView1.ContextMenuOpening
    Dim customMenuItem As RadMenuItem = New RadMenuItem()
    customMenuItem.Text = "Custom Data Operation"
    Dim separator As RadMenuSeparatorItem = New RadMenuSeparatorItem()
    e.ContextMenu.Items.Add(separator)
    e.ContextMenu.Items.Add(customMenuItem)
End Sub

The result of combining the approaches from this article is shown on the screenshot below:

gridview-context-menus-modifying-the-default-context-menu 001

See Also

Was this article helpful? Yes No
Thank you for your feedback!

Give article feedback

Tell us how we can improve this article

close
Dummy