Skip to main content

Popup and Context Menus

  • 6 minutes to read

This topic shows how to customize a Data Grid’s built-in popup menus, override the default menu item actions, and use events to create custom popup menus.

Note

This topic does not cover in-place editors’ menu items.

Built-In Context Menus

The Data Grid’s Grid Views and Banded Grid Views have the following built-in context menus:

View properties can affect the availability of items in these menus. For example, when you disable the GridOptionsCustomization.AllowSort option, the View deactivates the Sort Ascending and Sort Descending items in the Column Header Menu.

Not all menu items can be hidden through View properties. To control menu item visibility, use the GridView.PopupMenuShowing event as demonstrated in the following topic: Modify Built-In Context Menus.

Custom Context Menus

You can create a custom context menu (PopupMenu) and invoke this menu when a user right-clicks a grid element. This technique has the following common concepts:

  • Use the BarManager designer to create a popup menu and populate it with items as demonstrated in the following help topic: Popup Menus.

    WinForms Popup Menu Designer

  • The GridView.PopupMenuShowing event allows you to invoke a custom context menu or modify built-in menus.

  • The e.HitInfo property returns information about the control area right-clicked by a user (a GridHitInfo object for the Data Grid).

  • You can invoke your context menu instead of a built-in menu or for a custom area:

    void gridView1_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e) {
        // Invoke a custom menu instead of the built-in column header context menu:
        if (e.MenuType == GridMenuType.Column)
            e.ShowCustomMenu(ColumnPopupMenu);
        // Invoke a custom menu when a user right-clicks the filter panel:
        if (e.HitInfo.InFilterPanel)
            e.ShowCustomMenu(FilterPanelPopupMenu);
    }
    
  • We recommend that you assign the e.HitInfo property to the PopupMenuBase.Tag property to pass information about a clicked element to your menu.

    void gridView1_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e) {
        // ...
        MyMenu.Tag = e.HitInfo;
        e.ShowCustomMenu(MyMenu);
    }
    
    GridHitInfo GetHitInfo(BarItemLink link) {
        PopupMenu menu = link.LinkedObject as PopupMenu;
        return menu.Tag as GridHitInfo;
    }
    void barButtonItem_ItemClick(object sender, ItemClickEventArgs e) {
        GridHitInfo info = GetHitInfo(e.Link);
        info.View.ShowFilterEditor(info.Column);
    }
    

Show a Custom Popup Menu for Grid Elements

The following code sample invokes a custom context menu when a user right-clicks a column header:

Create Custom Context Menus

  1. Add a BarManager to the form and create a custom PopupMenu.

  2. Handle the GridView.PopupMenuShowing event and call the e.ShowCustomMenu method to display your custom menu instead of the default header menu.

void gridView1_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e) {
    if (e.MenuType == GridMenuType.Column) {
        popupMenu_Column.Tag = e.HitInfo;
        popupMenu_Column.MenuCaption = $"{e.HitInfo.Column}";

        e.ShowCustomMenu(popupMenu_Column);
    }
}

GridHitInfo GetHitInfo(BarItemLink link) {
    PopupMenu menu = link.LinkedObject as PopupMenu;
    return menu.Tag as GridHitInfo;
}
void barButtonItem_Filter_ItemClick(object sender, ItemClickEventArgs e) {
    GridHitInfo info = GetHitInfo(e.Link);
    info.View.ShowFilterEditor(info.Column);
}

void barButtonItem_ColumnChooser_ItemClick(object sender, ItemClickEventArgs e) {
    GridHitInfo info = GetHitInfo(e.Link);
    info.View.ShowCustomization();
}

Add a Context Menu for Grid Empty Space

The GridView.PopupMenuShowing event allows you to invoke a context menu when a user right-clicks an area that does not contain any grid elements:

WInForms Grid - Context Menu for Empty Space

The e.MenuType property returns User when a user right-clicks an empty space, Filter Panel, Group Row, or Header Panel Button. Create a condition applied to empty area only and use the e.ShowCustomMenu method to invoke your context menu:

void gridView1_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e) {
    if (e.MenuType == GridMenuType.User && !e.HitInfo.InFilterPanel 
        && !e.HitInfo.InGroupRow && e.HitInfo.HitTest != GridHitTest.ColumnButton) {
        popupMenu_Empty.Tag = e.HitInfo;
        e.ShowCustomMenu(popupMenu_Empty);
    }
}

Modify Built-In Popup Menus

You can add, modify, and remove items displayed in default context menus. Use this technique only if custom context menus do not suit your requirements. Refer to the following help topic for more information: Modify Built-In Context Menus.

The Data Grid paints its menus according to system settings, without taking into account the current skin.

To apply a skin to the Data Grid’s menus, place a BarManager or RibbonControl component onto the form. This component is automatically bound to the Data Grid’s EditorContainer.MenuManager property. The RibbonControl/BarManager now manages the display and behavior of the grid’s menus, and applies the selected paint scheme (skin) to them.

The following image shows a column header menu painted using different themes.

popupmenus-differentpaintschemes.png

You can use the Project Settings Page or the DefaultLookAndFeel component to customize the default paint scheme for all controls, including the data grid’s popup menus.

Cheat Sheets and Best Practices

Read the following quick-reference guide for general information and examples:

Context Menus - WinForms Cheat Sheets