Back to Devexpress

Popup and Context Menus

windowsforms-3505-controls-and-libraries-data-grid-popup-menus.md

latest9.6 KB
Original Source

Popup and Context Menus

  • Feb 28, 2024
  • 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.

  • 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:

  • We recommend that you assign the e.HitInfo property to the PopupMenuBase.Tag property to pass information about a clicked element to your menu.

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:

  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.

csharp
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();
}
vb
Private Sub gridView1_PopupMenuShowing(ByVal sender As Object, ByVal e As PopupMenuShowingEventArgs)
    If e.MenuType = GridMenuType.Column Then
        popupMenu_Column.Tag = e.HitInfo
        popupMenu_Column.MenuCaption = $"{e.HitInfo.Column}"

        e.ShowCustomMenu(popupMenu_Column)
    End If
End Sub

Private Function GetHitInfo(ByVal link As BarItemLink) As GridHitInfo
    Dim menu As PopupMenu = TryCast(link.LinkedObject, PopupMenu)
    Return TryCast(menu.Tag, GridHitInfo)
End Function

Private Sub barButtonItem_Filter_ItemClick(ByVal sender As Object, ByVal e As ItemClickEventArgs)
    Dim info As GridHitInfo = GetHitInfo(e.Link)
    info.View.ShowFilterEditor(info.Column)
End Sub

Private Sub barButtonItem_ColumnChooser_ItemClick(ByVal sender As Object, ByVal e As ItemClickEventArgs)
    Dim info As GridHitInfo = GetHitInfo(e.Link)
    info.View.ShowCustomization()
End Sub

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:

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:

csharp
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);
    }
}
vb
Private Sub gridView1_PopupMenuShowing(ByVal sender As Object, ByVal e As PopupMenuShowingEventArgs)
    If e.MenuType = GridMenuType.User AndAlso Not e.HitInfo.InFilterPanel AndAlso Not e.HitInfo.InGroupRow AndAlso e.HitInfo.HitTest <> GridHitTest.ColumnButton Then
        popupMenu_Empty.Tag = e.HitInfo
        e.ShowCustomMenu(popupMenu_Empty)
    End If
End Sub

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.

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