PopupMenuShowingEventArgs.Menu Property
Gets or sets the popup menu that is about to be displayed.
Namespace: DevExpress.XtraGrid.Views.Grid
Assembly: DevExpress.XtraGrid.v19.2.dll
Declaration
Property Value
Type | Description |
---|---|
DevExpress.XtraGrid.Menu.GridViewMenu | The menu that is about to be displayed. |
Remarks
The Menu property allows you to add, remove and customize items in the currently processed popup menu. To identify the type of the menu, use PopupMenuShowingEventArgs.MenuType.
The Data Grid does not display empty popup menus. Thus, if you remove all items from the menu, this menu will not be displayed after your PopupMenuShowing event handler is complete.
When you right-click within a row or group row, the Data Grid creates an empty menu and fires the PopupMenuShowing event (the event’s PopupMenuShowingEventArgs.MenuType parameter is set to Row). If you leave this menu intact (empty), it will not be displayed. To add new menu items to this and other menus, use the Menu.Items.Add method. See an example below.
To access the existing menu items, you can iterate through the Menu.Items collection, or use the Menu.Find and Menu.FindAll methods. To hide or remove certain menu items, use the Menu.Hide and Menu.Remove methods. When searching for a target menu item, you need to know the menu item identifier, which you can find by reading the item’s DXMenuItem.Tag property. You can also see the Menu Item Identifiers section below to learn the available menu item identifiers for the built-in menus.
Menu Item Objects
The menu items can be the following objects:
- DXMenuItem - A regular button.
- DXSubMenuItem - A sub-menu.
- DXMenuCheckItem - A check button.
Use these objects’ settings to change item captions, images, visibility, enabled state, etc.
The DXMenuItem.Tag properties store the menu item identifiers for the default menu items.
When you create a custom menu item, use the item’s DXMenuItem.Tag property to store any custom data with which you may need to identify the item or process the item’s click event.
Menu Item Identifiers
The four tables below show the menu item identifiers (contents of the DXMenuItem.Tag properties) in Data Grid’s built-in menus.
Column Header Menu (DevExpress.XtraGrid.Menu.GridViewColumnMenu)
When handling the PopupMenuShowing and GridMenuItemClick events, you can identify this menu type using the e.MenuType event parameter, which is set to GridMenuType.Column
The Tag properties of the items in this menu can be the following objects:
DevExpress.XtraGrid.Localization.GridStringId
DevExpress.XtraEditors.Controls.StringId
DevExpress.XtraGrid.ColumnGroupInterval
Menu Item | Tag |
---|---|
Full Expand | DevExpress.XtraGrid.Localization.GridStringId.MenuGroupPanelFullExpand |
Full Collapse | DevExpress.XtraGrid.Localization.GridStringId.MenuGroupPanelFullCollapse |
Sort Ascending | DevExpress.XtraGrid.Localization.GridStringId.MenuColumnSortAscending |
Sort Descending | DevExpress.XtraGrid.Localization.GridStringId.MenuColumnSortDescending |
Sort by Summary (sub-menu) | DevExpress.XtraGrid.Localization.GridStringId.MenuColumnSortGroupBySummaryMenu The Tag properties of this submenu’s items contain GridGroupSummaryItem objects, which identify corresponding summary functions. |
Group By This Column | DevExpress.XtraGrid.Localization.GridStringId.MenuColumnGroup |
UnGroup | DevExpress.XtraGrid.Localization.GridStringId.MenuColumnUnGroup |
Hide Group By Box | DevExpress.XtraGrid.Localization.GridStringId.MenuColumnGroupBox |
Show Group By Box | DevExpress.XtraGrid.Localization.GridStringId.MenuColumnGroupBox |
Split | DevExpress.XtraGrid.Localization.GridStringId.MenuShowSplitItem |
Remove Split | DevExpress.XtraGrid.Localization.GridStringId.MenuHideSplitItem |
Group Interval (sub-menu) | DevExpress.XtraGrid.Localization.GridStringId.MenuColumnGroupIntervalMenu |
Day | DevExpress.XtraGrid.ColumnGroupInterval.Default |
Month | DevExpress.XtraGrid.ColumnGroupInterval.DateMonth |
Year | DevExpress.XtraGrid.ColumnGroupInterval.DateYear |
Smart | DevExpress.XtraGrid.ColumnGroupInterval.DateRange |
Group Summary Editor… | DevExpress.XtraGrid.Localization.GridStringId.MenuColumnGroupSummaryEditor |
Hide This Column | DevExpress.XtraGrid.Localization.GridStringId.MenuColumnRemoveColumn |
Column Chooser | DevExpress.XtraGrid.Localization.GridStringId.MenuColumnColumnCustomization |
Best Fit | DevExpress.XtraGrid.Localization.GridStringId.MenuColumnBestFit |
Best Fit (all columns) | DevExpress.XtraGrid.Localization.GridStringId.MenuColumnBestFitAllColumns |
Filter Editor… | DevExpress.XtraGrid.Localization.GridStringId.MenuColumnFilterEditor |
Show Find Panel | DevExpress.XtraGrid.Localization.GridStringId.MenuColumnFindFilterShow |
Hide Find Panel | DevExpress.XtraGrid.Localization.GridStringId.MenuColumnFindFilterHide |
Show Auto Filter Row | DevExpress.XtraGrid.Localization.GridStringId.MenuColumnAutoFilterRowShow |
Hide Auto Filter Row | DevExpress.XtraGrid.Localization.GridStringId.MenuColumnAutoFilterRowHide |
Conditional Formatting (sub-menu) | DevExpress.XtraGrid.Localization.GridStringId.MenuColumnConditionalFormatting |
Highlight Cell Rules (sub-menu) | DevExpress.XtraEditors.Controls.StringId.FormatRuleMenuItemHighlightCellRules |
Greater Than… | DevExpress.XtraEditors.Controls.StringId.FormatRuleMenuItemGreaterThan |
Less Than… | DevExpress.XtraEditors.Controls.StringId.FormatRuleMenuItemLessThan |
Between… | DevExpress.XtraEditors.Controls.StringId.FormatRuleMenuItemBetween |
Equal To… | DevExpress.XtraEditors.Controls.StringId.FormatRuleMenuItemEqualTo |
Text that Contains… | DevExpress.XtraEditors.Controls.StringId.FormatRuleMenuItemTextThatContains |
A Date Occurring… | DevExpress.XtraEditors.Controls.StringId.FormatRuleMenuItemDateOccurring |
Custom Condition… | DevExpress.XtraEditors.Controls.StringId.FormatRuleMenuItemCustomCondition |
Top/Bottom Rules (sub-menu) | DevExpress.XtraEditors.Controls.StringId.FormatRuleMenuItemTopBottomRules |
Top 10 Items… | DevExpress.XtraEditors.Controls.StringId.FormatRuleMenuItemTop10Items |
Bottom 10 Items… | DevExpress.XtraEditors.Controls.StringId.FormatRuleMenuItemBottom10Items |
Unique/Duplicate Rules (sub-menu) | DevExpress.XtraEditors.Controls.StringId.FormatRuleMenuItemUniqueDuplicateRules |
Unique Values… | DevExpress.XtraEditors.Controls.StringId.FormatRuleMenuItemUnique |
Duplicate Values… | DevExpress.XtraEditors.Controls.StringId.FormatRuleMenuItemDuplicate |
Manage Rules… | DevExpress.XtraEditors.Controls.StringId.FormatRuleMenuItemManageRules |
Show Footer | DevExpress.XtraGrid.Localization.GridStringId.MenuFooterShow |
Hide Footer | DevExpress.XtraGrid.Localization.GridStringId.MenuFooterHide |
Group Panel Menu (DevExpress.XtraGrid.Menu.GridViewGroupPanelMenu)
When handling the PopupMenuShowing and GridMenuItemClick events, you can identify this menu type using the e.MenuType event parameter, which is set to GridMenuType.Group
The Tag properties of the items in this menu contain DevExpress.XtraGrid.Localization.GridStringId enumeration values.
Menu Item | Tag |
---|---|
Full Expand | DevExpress.XtraGrid.Localization.GridStringId.MenuGroupPanelFullExpand |
Full Collapse | DevExpress.XtraGrid.Localization.GridStringId.MenuGroupPanelFullCollapse |
Clear Grouping | DevExpress.XtraGrid.Localization.GridStringId.MenuGroupPanelClearGrouping |
Hide Group By Box | DevExpress.XtraGrid.Localization.GridStringId.MenuColumnGroupBox |
Split | DevExpress.XtraGrid.Localization.GridStringId.MenuShowSplitItem |
Remove Split | DevExpress.XtraGrid.Localization.GridStringId.MenuHideSplitItem |
Summary Menu (DevExpress.XtraGrid.Menu.GridViewFooterMenu)
When handling the PopupMenuShowing and GridMenuItemClick events, you can identify this menu type using the e.MenuType event parameter, which is set to GridMenuType.Summary
The Tag properties of the items in this menu contain DevExpress.XtraGrid.Localization.GridStringId enumeration values.
Menu Item | Tag |
---|---|
Add New Summary (sub-menu) | DevExpress.XtraGrid.Localization.GridStringId.MenuFooterAddSummaryItem |
Sum | DevExpress.XtraGrid.Localization.GridStringId.MenuFooterSum |
Min | DevExpress.XtraGrid.Localization.GridStringId.MenuFooterMin |
Max | DevExpress.XtraGrid.Localization.GridStringId.MenuFooterMax |
Count | DevExpress.XtraGrid.Localization.GridStringId.MenuFooterCount |
Average | DevExpress.XtraGrid.Localization.GridStringId.MenuFooterAverage |
None | DevExpress.XtraGrid.Localization.GridStringId.MenuFooterNone |
Clear Summary Items | DevExpress.XtraGrid.Localization.GridStringId.MenuFooterClearSummaryItems |
Auto Filter Row Menu
When handling the PopupMenuShowing and GridMenuItemClick events, you can identify this menu type using the e.MenuType event parameter, which is set to GridMenuType.AutoFilter.
The Tag properties of the items in this menu contain DevExpress.XtraEditors.ColumnAutoFilterCondition enumeration values.
Menu Item | Tag |
---|---|
Clear Filter | null |
Equals | DevExpress.XtraEditors.ColumnAutoFilterCondition.Equals |
Does not equal | DevExpress.XtraEditors.ColumnAutoFilterCondition.DoesNotEqual |
Contains | DevExpress.XtraEditors.ColumnAutoFilterCondition.Contains |
Does not contain | DevExpress.XtraEditors.ColumnAutoFilterCondition.DoesNotContain |
Is like | DevExpress.XtraEditors.ColumnAutoFilterCondition.Like |
Is not like | DevExpress.XtraEditors.ColumnAutoFilterCondition.NotLike |
Begins with | DevExpress.XtraEditors.ColumnAutoFilterCondition.BeginsWith |
Ends with | DevExpress.XtraEditors.ColumnAutoFilterCondition.EndsWith |
Is greater than | DevExpress.XtraEditors.ColumnAutoFilterCondition.Greater |
Is greater than or equal to | DevExpress.XtraEditors.ColumnAutoFilterCondition.GreaterOrEqual |
Is less than | DevExpress.XtraEditors.ColumnAutoFilterCondition.Less |
Is less than or equal to | DevExpress.XtraEditors.ColumnAutoFilterCondition.LessOrEqual |
Example
The following example shows how to use the GridView.PopupMenuShowing event to create a custom menu in the Data Grid control. The created menu is displayed when you right-click within a data row or group row. It contains a ‘Rows’ submenu with a single “Delete this row” regular button, and a ‘Cell Merging’ check button.
A click on the ‘Delete this row’ button invokes the ColumnView.DeleteRow method. The ‘Cell Merging’ check button toggles the GridOptionsView.AllowCellMerge option.
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.Utils.Menu;
using DevExpress.XtraEditors;
private void gridView1_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e) {
GridView view = sender as GridView;
if (e.MenuType == DevExpress.XtraGrid.Views.Grid.GridMenuType.Row) {
int rowHandle = e.HitInfo.RowHandle;
// Delete existing menu items, if any.
e.Menu.Items.Clear();
// Add the Rows submenu with the 'Delete Row' command
e.Menu.Items.Add(CreateSubMenuRows(view, rowHandle));
// Add the 'Cell Merging' check menu item.
DXMenuItem item = CreateMenuItemCellMerging(view, rowHandle);
item.BeginGroup = true;
e.Menu.Items.Add(item);
}
}
DXMenuItem CreateSubMenuRows(GridView view, int rowHandle) {
DXSubMenuItem subMenu = new DXSubMenuItem("Rows");
string deleteRowsCommandCaption;
if (view.IsGroupRow(rowHandle))
deleteRowsCommandCaption = "&Delete rows in this group";
else
deleteRowsCommandCaption = "&Delete this row";
DXMenuItem menuItemDeleteRow = new DXMenuItem(deleteRowsCommandCaption, new EventHandler(OnDeleteRowClick), imageCollection1.Images[0]);
menuItemDeleteRow.Tag = new RowInfo(view, rowHandle);
menuItemDeleteRow.Enabled = view.IsDataRow(rowHandle) || view.IsGroupRow(rowHandle);
subMenu.Items.Add(menuItemDeleteRow);
return subMenu;
}
DXMenuCheckItem CreateMenuItemCellMerging(GridView view, int rowHandle) {
DXMenuCheckItem checkItem = new DXMenuCheckItem("Cell &Merging",
view.OptionsView.AllowCellMerge, null, new EventHandler(OnCellMergingClick));
checkItem.Tag = new RowInfo(view, rowHandle);
checkItem.ImageOptions.Image = imageCollection1.Images[1];
return checkItem;
}
void OnDeleteRowClick(object sender, EventArgs e) {
DXMenuItem menuItem = sender as DXMenuItem;
RowInfo ri = menuItem.Tag as RowInfo;
if (ri != null) {
string message = menuItem.Caption.Replace("&", "");
if (XtraMessageBox.Show(message + " ?", "Confirm operation", MessageBoxButtons.YesNo) != DialogResult.Yes)
return;
ri.View.DeleteRow(ri.RowHandle);
}
}
void OnCellMergingClick(object sender, EventArgs e) {
DXMenuCheckItem item = sender as DXMenuCheckItem;
RowInfo info = item.Tag as RowInfo;
info.View.OptionsView.AllowCellMerge = item.Checked;
}
class RowInfo {
public RowInfo(GridView view, int rowHandle) {
this.RowHandle = rowHandle;
this.View = view;
}
public GridView View;
public int RowHandle;
}
Example
The following code customizes the existing commands in the Data Grid’s Column Header Menu by handling the GridView.PopupMenuShowing event:
Captions of the “Sort Ascending” and “Sort Descending” commands are replaced with custom strings;
A few commands are hidden;
Two “Best Fit” commands are disabled.
using DevExpress.Utils.Menu;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Localization;
using DevExpress.XtraGrid.Views.Grid;
private void gridView1_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e) {
if(e.MenuType == GridMenuType.Column) {
string sortAsc = "A to Z";
string sortDesc = "Z to A";
if (e.HitInfo.Column.ColumnType == typeof(DateTime)) {
sortAsc = "Old to New";
sortDesc = "New to Old";
}
DXMenuItem itemAsc = e.Menu.Find(GridStringId.MenuColumnSortAscending); // "Sort Ascending"
if (itemAsc != null)
itemAsc.Caption = sortAsc;
DXMenuItem itemDesc = e.Menu.Find(GridStringId.MenuColumnSortDescending); // "Sort Descending"
if (itemDesc != null)
itemDesc.Caption = sortDesc;
e.Menu.Hide(GridStringId.MenuFooterHide); //"Hide Footer"
e.Menu.Hide(GridStringId.MenuColumnFilterEditor); // "Filter Editor..."
e.Menu.Hide(GridStringId.MenuColumnFindFilterShow); // "Show Find Panel"
e.Menu.Hide(GridStringId.MenuColumnFindFilterHide); // "Hide Find Panel"
e.Menu.Hide(GridStringId.MenuColumnAutoFilterRowHide); // "Hide Auto Filter Row"
e.Menu.Hide(GridStringId.MenuColumnAutoFilterRowShow); // "Show Auto Filter Row"
DXMenuItem itemBestFit = e.Menu.Find(GridStringId.MenuColumnBestFit); // "Best Fit"
if (itemBestFit != null)
itemBestFit.Enabled = false;
DXMenuItem itemBestFitAll = e.Menu.Find(GridStringId.MenuColumnBestFitAllColumns); // "Best Fit All Columns"
if (itemBestFitAll != null)
itemBestFitAll.Enabled = false;
}
}
Example
The following sample code handles the GridView.PopupMenuShowing event to customize the column header context menu before it is displayed. The code clears default menu items and adds a new command that locks a clicked column’s position.
using DevExpress.XtraGrid.Menu;
using DevExpress.Utils.Menu;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
// ...
private void bandedGridView1_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e) {
if(e.MenuType == GridMenuType.Column) {
GridViewColumnMenu menu = e.Menu as GridViewColumnMenu;
menu.Items.Clear();
if(menu.Column != null) {
menu.Items.Add(CreateCheckItem("Lock this column", menu.Column, null));
}
}
}
// Creates a menu item.
DXMenuCheckItem CreateCheckItem(string caption, GridColumn column, Image image) {
DXMenuCheckItem item = new DXMenuCheckItem(caption,
!column.OptionsColumn.AllowMove, image, new EventHandler(OnCanMoveItemClick));
item.Tag = new MenuColumnInfo(column);
return item;
}
// Menu item click handler.
void OnCanMoveItemClick(object sender, EventArgs e) {
DXMenuCheckItem item = sender as DXMenuCheckItem;
MenuColumnInfo info = item.Tag as MenuColumnInfo;
if(info == null) return;
info.Column.OptionsColumn.AllowMove = !item.Checked;
}
class MenuColumnInfo {
public MenuColumnInfo(GridColumn column) {
this.Column = column;
}
public GridColumn Column;
}
Related GitHub Examples
The following code snippets (auto-collected from DevExpress Examples) contain references to the Menu property.
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.