Modify Built-In Context Menus
- 4 minutes to read
This topic describes how to change default context menus. You can add, modify, and remove menu items. Use this technique only if custom context menus do not suit your requirements.
Add Custom Items to Built-In Menus
Handle the GridView.PopupMenuShowing event and add custom items to the e.Menu.Items
collection. You can use the following objects as custom items:
- DXMenuItem – A regular button.
- DXSubMenuItem – A sub-menu.
- DXMenuCheckItem – A check button.
Example
The following code sample creates a menu item that fixes the column to the left. This item is added to the column header’s context menu next to the Hide This Column item:
void gridView1_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e) {
if (e.MenuType == GridMenuType.Column) {
DXMenuItem item = new DXMenuItem("Fix/Unfix This Column", (s, args) => {
GridColumn column = (s as DXMenuItem).Tag as GridColumn;
if (column.Fixed == FixedStyle.Left)
column.Fixed = FixedStyle.None;
else column.Fixed = FixedStyle.Left;
});
item.Tag = e.HitInfo.Column;
int index = e.Menu.Items.IndexOf(e.Menu.Find(GridStringId.MenuColumnRemoveColumn));
e.Menu.Items.Insert(index + 1, item);
}
}
Customize Default Menu Items
You can use the GridView.PopupMenuShowing event to customize menu items (change their captions, visibility, state, etc.).
The e.Menu property returns the invoked context menu. You can iterate through the e.Menu.Items
collection or call the e.Menu.Find
and e.Menu.FindAll
methods to access individual menu items. These methods accept menu item identifiers as parameters. Refer to the following help topic for more information on menu item identification: e.Menu.
Example
The following code sample disables the Column Chooser item in the column header’s context menu:
void gridView1_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e) {
if (e.MenuType == GridMenuType.Column) {
DXMenuItem menuItem = e.Menu.Find(GridStringId.MenuColumnColumnCustomization);
if (menuItem != null)
menuItem.Enabled = false;
}
}
Remove Default Menu Items
Handle the GridView.PopupMenuShowing event and and use e.Menu.Hide
and e.Menu.Remove
methods to hide or remove menu items. These methods accept menu item identifiers as parameters. Refer to the following help topic for more information on menu item identification: e.Menu.
Example
The following code sample removes items related to Filter Editor, Find Panel, and Auto Filter Row from the column header’s context menu:
void gridView1_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e) {
if (e.MenuType == GridMenuType.Column) {
e.Menu.Remove(GridStringId.MenuColumnFilterEditor); // "Filter Editor..."
e.Menu.Remove(GridStringId.MenuColumnFindFilterShow); // "Show Find Panel"
e.Menu.Remove(GridStringId.MenuColumnFindFilterHide); // "Hide Find Panel"
e.Menu.Remove(GridStringId.MenuColumnAutoFilterRowHide); // "Hide Auto Filter Row"
e.Menu.Remove(GridStringId.MenuColumnAutoFilterRowShow); // "Show Auto Filter Row"
}
}
Change the Default Behavior of Menu Items
A click on a menu item in the built-in context menus raises the GridView.GridMenuItemClick event. You can handle this event to:
- Execute custom actions.
- Cancel the default action (set the e.Handled property to
true
).
Example
The following code sample shows a warning when a user selects the Hide This Column item in the column header menu. The No button in the message box cancels the action.
void gridView1_GridMenuItemClick(object sender, GridMenuItemClickEventArgs e) {
if(e.MenuType == GridMenuType.Column) {
GridStringId value = e.DXMenuItem.Tag is GridStringId ? (GridStringId)e.DXMenuItem.Tag : 0 ;
if (value == GridStringId.MenuColumnRemoveColumn) { // "Hide This Column"
if(MessageBox.Show("Do you want to hide this column?", "Warning", MessageBoxButtons.YesNo)== DialogResult.No) {
e.Handled = true; // Cancel the default action
}
}
}
}