GridView.PopupMenuShowing Event

Enables you to customize or prohibit built-in context menus.

Namespace: DevExpress.XtraGrid.Views.Grid

Assembly: DevExpress.XtraGrid.v20.1.dll

Declaration

[DXCategory("Behavior")]
public event PopupMenuShowingEventHandler PopupMenuShowing
<DXCategory("Behavior")>
Public Event PopupMenuShowing As PopupMenuShowingEventHandler

Event Data

The PopupMenuShowing event's handler receives an argument of the PopupMenuShowingEventArgs type. The following properties provide information specific to this event:

Property Description
Allow Gets or sets whether to enable the Grid's popup menu.
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 the position where the menu is to be invoked.

Remarks

The Grid View provides built-in context menus for the following elements:

To learn about the default contents of these menus, see Popup Menus.

By handling the PopupMenuShowing event, you can do the following:

  • Prohibit display of these menus dynamically, according to your logic. Use the PopupMenuShowingEventArgs.Allow event parameter.
  • Add custom items (regular buttons, check buttons and sub-menus) to these menus. Use the e.Menu.Items.Add method.
  • Customize the existing menu items (change their captions, images, visibility, etc.). To learn how to access the existing items, see PopupMenuShowingEventArgs.Menu.

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.

GridView_ShowGridMenu

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;
}

Example 2

This example illustrates how to disable or remove unwanted conditions from the Automatic Filtering Row. The sample below uses the XtraLocalizer<T>.GetLocalizedString method to remove common numeric filter conditions (less than, greater or equal than, etc.) for the "Postal Code" Grid column.

Auto Filter Row - Hide Conditions


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;
    }
}
See Also