GridView.PopupMenuShowing Event
Allows you to customize built-in context menus or invoke custom menus.
Namespace: DevExpress.XtraGrid.Views.Grid
Assembly: DevExpress.XtraGrid.v24.1.dll
NuGet Packages: DevExpress.Win.Grid, DevExpress.Win.Navigation
Declaration
Event Data
The PopupMenuShowing event's data class is PopupMenuShowingEventArgs. The following properties provide information specific to this event:
Property | Description |
---|---|
Allow | Gets or sets whether to display the context menu. Inherited from BasePopupMenuShowingEventArgs. |
HitInfo | Gets an object that identifies a clicked element. |
Menu | Gets or sets the popup menu that is about to be displayed. |
MenuType | Gets the type of the Grid View’s menu to be invoked. |
Point | Gets or sets coordinates of the invoked context menu (top-left corner) relative to the parent control. Inherited from BasePopupMenuShowingEventArgs. |
ScreenPoint | Gets coordinates of the invoked context menu (top-left corner) relative to the screen. Inherited from BasePopupMenuShowingEventArgs. |
The event data class exposes the following methods:
Method | Description |
---|---|
ShowCustomMenu(IDXDropDownControlEx) | Invokes a custom context menu instead of the control’s menu. Inherited from BasePopupMenuShowingEventArgs. |
ShowCustomMenu(ContextMenuStrip) | Invokes a custom context menu instead of the control’s menu. Inherited from BasePopupMenuShowingEventArgs. |
Remarks
The PopupMenuShowing event allows you to perform the following tasks:
- Invoke custom context menus for grid elements (the e.ShowCustomMenu method).
- Add custom items (regular buttons, check buttons and sub-menus) to popup menus (the e.Menu.Items.Add method).
- Customize existing menu items (change their captions, images, visibility, etc.). Refer to the following help topic for more information on how to access menu items: e.Menu.
- Prohibit the display of popup menus dynamically according to your logic. To disable a popup menu, use the e.Allow property.
Refer to the following help topic for more information: Popup and Context Menus.
Examples
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();
}
This example illustrates how to disable or remove unwanted conditions from the Automatic Filtering Row Menu for a “Postal Code” grid column.
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraEditors.Controls;
private void GridView1_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e) {
GridView view = sender as GridView;
if (e.MenuType == GridMenuType.AutoFilter && view.FocusedColumn == view.Columns["PostalCode"]) {
//remove conditions
e.Menu.Remove(ColumnAutoFilterCondition.Less);
e.Menu.Remove(ColumnAutoFilterCondition.LessOrEqual);
e.Menu.Remove(ColumnAutoFilterCondition.Greater);
e.Menu.Remove(ColumnAutoFilterCondition.GreaterOrEqual);
//disable conditions
if (e.Menu.Find(ColumnAutoFilterCondition.Contains) != null)
e.Menu.Find(ColumnAutoFilterCondition.Contains).Enabled = false;
if (e.Menu.Find(ColumnAutoFilterCondition.DoesNotContain) != null)
e.Menu.Find(ColumnAutoFilterCondition.DoesNotContain).Enabled = false;
}
}
Important
The e.Menu.Remove
method accepts DevExpress.XtraGrid.Localization.GridStringId
as a parameter. The method does not work for menu items with dynamic names (for example, “Group Panel Hide”, “Group Panel Show”).
// This code has no effect.
e.Menu.Remove(DevExpress.XtraGrid.Localization.GridStringId.MenuGroupPanelHide);
e.Menu.Remove(DevExpress.XtraGrid.Localization.GridStringId.MenuGroupPanelShow);
// This code works as expected.
e.Menu.Remove(DevExpress.XtraGrid.Localization.GridStringId.MenuColumnGroupBox);
Related GitHub Examples
The following code snippet (auto-collected from DevExpress Examples) contains a reference to the PopupMenuShowing event.
Note
The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.