Context Menus
- 3 minutes to read
The GridControl can display popup menus that enable an end-user to manage data (apply grouping and sorting, display summaries, etc.) and customize a View by showing and hiding its UI elements. All these menus can be customized. For example, you can remove existing menu items and/or add new items.
Context Menus Overview
Use the GridViewBase.GridMenu property to obtain the context menu currently being displayed within a View.
Menu Type | Description | Availability | Image |
---|---|---|---|
Group Panel | This menu is shown when an end-user right-clicks within the Group Panel. | ||
Column | This menu is shown when an end-user right-clicks a column header. | ||
Row Cell | This menu is shown when an end-user right-clicks a data cell. You should manually create this menu. For an example, see How to: Display a Context Menu for Data Cells. | ||
Summary | This menu is shown when an end-user right-clicks within the Summary Panel. | ||
Fixed Summary | This menu is shown when an end-user right-clicks within the Fixed Summary Panel. |
To obtain the current column, view or grid, use the dxg:GridPopupMenu.GridMenuInfo attached property.
GridControl displays specific context menus in Compact Mode.
Customizing Context Menus
Change Menu Item Names
When customizing a menu (e.g. removing or moving default items), menu items are referred to by their names. Default menu item names are listed in DefaultColumnMenuItemNames and DefaultSummaryMenuItemNames.
Customize Menu Items in Code
Handle the DataViewBase.ShowGridMenu event to dynamically customize grid menus at runtime.
Add, Move, and Remove Menu Items
A View provides multiple properties that allow you to customize its context menus by adding new menu items or removing existing items. These properties return a BarManagerActionCollection object that provides multiple methods, used to manage menu items contained within a context menu:
Menu Type | Property |
---|---|
Group Panel | |
Column | |
Band | |
Row Cell | |
Summary | |
Group Row | |
Compact Panel | TableView TableView.CompactModeFilterElementMenuCustomizations TableView.CompactModeFilterMergeElementMenuCustomizations TableView.CompactModeSortElementMenuCustomizations TreeListView TreeListView.CompactModeFilterElementMenuCustomizations TreeListView.CompactModeFilterMergeElementMenuCustomizations |
Note
This feature is not available in DXTreeList when Hierarchical Data Templates are used.
Example 1: Add Menu Items
The following example demonstrates how to add a custom menu item to the context menu of a grid column. For this, it is necessary to add a corresponding bar item (e.g. BarCheckItem) to the DataViewBase.ColumnMenuCustomizations collection, and specify different item properties. Also, you can insert this item into a specific position by attaching the BarItemLinkActionBase.ItemLinkIndex property to it.
Note
A complete sample project is available at https://github.com/DevExpress-Examples/how-to-customize-a-grids-context-menu-e1925.
<dxg:TableView.ColumnMenuCustomizations>
<dxb:BarCheckItem Name="checkItem1" Content="Checked" IsChecked="True" dxb:BarItemLinkActionBase.ItemLinkIndex="0" />
<dxb:BarItemLinkSeparator dxb:BarItemLinkActionBase.ItemLinkIndex="1" />
</dxg:TableView.ColumnMenuCustomizations>
private void customItem1_CheckedChanged(object sender, ItemClickEventArgs e) {
// do something...
}
Example 2: Remove Menu Items
The following example demonstrates how to remove a specific item from the context menu, which is shown for the Total Summary panel. For this, the RemoveBarItemAndLinkAction object is added to the DataViewBase.TotalSummaryMenuCustomizations collection, and the name of the bar item is specified by the corresponding field of the DefaultSummaryMenuItemNames class.
Note
A complete sample project is available at https://github.com/DevExpress-Examples/how-to-customize-a-grids-context-menu-e1925.
<dxg:TableView.TotalSummaryMenuCustomizations>
<dxb:RemoveBarItemAndLinkAction ItemName="{x:Static dxg:DefaultSummaryMenuItemNames.Customize}" />
</dxg:TableView.TotalSummaryMenuCustomizations>