windowsforms-3505-controls-and-libraries-data-grid-popup-menus.md
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.
The Data Grid’s Grid Views and Banded Grid Views have the following built-in context menus:
Column Header Menu (right-click a column header)
Group Panel Menu (right-click the Group Panel)
Group Row Menu (right-click a Group Row)
Summary Menu (right-click the total summary footer and group summary footers)
Auto-Filter Row Menu (click a condition selector in the Auto-Filter Row)
Row Menu (right-click a data row)
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.
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.
The following code sample invokes a custom context menu when a user right-clicks a column header:
Add a BarManager to the form and create a custom PopupMenu.
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();
}
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
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:
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);
}
}
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
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.
Read the following quick-reference guide for general information and examples: