New to Telerik UI for WinForms? Download free 30-day trial

Conditional Custom Context Menus

Applications may need to provide specific individual context menus depending on the element that was clicked. Context menus make a great amount of functionality available without burdening the user with additional screen transitions or extra controls on the screen. The example below demonstrates how to dynamically add different custom context menus, depending on the part of the grid that was clicked.

To set a custom context menu to appear every time the user right-clicks the RadGridView, regardless of the element of the control they click, see Custom Context Menus.

Example

In this example, two different context menus are created and attached to cells in the second and third grid columns. If the user right-clicks in a cell the second column, they will get the first custom context menu. If they click in the third column, they will get the second context menu. If they right-click any other element of the RadGridView, the default context menu will be shown.

Start by creating the context menus, initializing its items, and subscribing to the context menu events that you want to handle. Then in the ContextMenuOpening event handler check the context menu provider and assign the corresponding context menu.

private RadContextMenu firstContextMenu = new RadContextMenu();
private RadContextMenu secondContextMenu = new RadContextMenu();    
void Form1_Load(object sender, EventArgs e)
{
    radGridView1.MasterTemplate.BestFitColumns();
    RadMenuItem firstContextMenuItem1 = new RadMenuItem("First menu - Item 1");
    firstContextMenuItem1.ForeColor = Color.Red;
    firstContextMenuItem1.Click += new EventHandler(firstContextMenuItem1_Click);
    RadMenuItem firstContextMenuItem2 = new RadMenuItem("First menu - Item 2");
    firstContextMenuItem2.Click += new EventHandler(firstContextMenuItem2_Click);
    firstContextMenu.Items.Add(firstContextMenuItem1);
    firstContextMenu.Items.Add(firstContextMenuItem2);
    RadMenuItem secondContextMenuItem1 = new RadMenuItem("Second menu - Item 1");
    secondContextMenuItem1.ForeColor = Color.LightBlue;
    secondContextMenuItem1.Click += new EventHandler(secondContextMenuItem1_Click);
    RadMenuItem secondContextMenuItem2 = new RadMenuItem("Second menu - Item 2");
    secondContextMenuItem2.Click += new EventHandler(secondContextMenuItem2_Click);
    secondContextMenu.Items.Add(secondContextMenuItem1);
    secondContextMenu.Items.Add(secondContextMenuItem2);
}
void radGridView1_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e)
{
    GridDataCellElement cell = e.ContextMenuProvider as GridDataCellElement;
    if (cell == null)
    {
        return;
    }
    //set the first context menu to be displayed for cells in the second column   
    if (cell.ColumnIndex == 1)
    {
        e.ContextMenu = firstContextMenu.DropDown;
    }
    //set the second context menu to be displayed for cells in the third column   
    else if (cell.ColumnIndex == 2)
    {
        e.ContextMenu = secondContextMenu.DropDown;
    }
}