Back to Devexpress

Modify Built-In Context Menus

windowsforms-404837-controls-and-libraries-data-grid-popup-and-context-menus-modify-built-in-menus.md

latest8.1 KB
Original Source

Modify Built-In Context Menus

  • Feb 28, 2024
  • 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:

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:

csharp
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);
    }
}
vb
Private Sub gridView1_PopupMenuShowing(ByVal sender As Object, ByVal e As PopupMenuShowingEventArgs)
    If e.MenuType = GridMenuType.Column Then
        Dim item As DXMenuItem = New DXMenuItem("Fix/Unfix This Column", Function(s, args)
            Dim column As GridColumn = TryCast((TryCast(s, DXMenuItem)).Tag, GridColumn)

            If column.Fixed = FixedStyle.Left Then
                column.Fixed = FixedStyle.None
            Else
                column.Fixed = FixedStyle.Left
            End If
        End Function)
        item.Tag = e.HitInfo.Column
        Dim index As Integer = e.Menu.Items.IndexOf(e.Menu.Find(GridStringId.MenuColumnRemoveColumn))
        e.Menu.Items.Insert(index + 1, item)
    End If
End Sub

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:

csharp
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;
    }
}
vb
Private Sub gridView1_PopupMenuShowing(ByVal sender As Object, ByVal e As PopupMenuShowingEventArgs)
    If e.MenuType = GridMenuType.Column Then
        Dim menuItem As DXMenuItem = e.Menu.Find(GridStringId.MenuColumnColumnCustomization)
        If menuItem IsNot Nothing Then menuItem.Enabled = False
    End If
End Sub

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:

csharp
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"
    }
}
vb
Private Sub gridView1_PopupMenuShowing(ByVal sender As Object, ByVal e As PopupMenuShowingEventArgs)
    If e.MenuType = GridMenuType.Column Then
        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"
    End If
End Sub

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.

csharp
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
            }
        }
    }
}
vb
Private Sub GridView1_GridMenuItemClick(sender As Object, e As GridMenuItemClickEventArgs) Handles GridView1.GridMenuItemClick
    If e.MenuType = GridMenuType.Column Then
        Dim value As GridStringId = If(TypeOf e.DXMenuItem.Tag Is GridStringId, CType(e.DXMenuItem.Tag, GridStringId), 0)
        If value = GridStringId.MenuColumnRemoveColumn Then ' "Hide This Column"
            If MessageBox.Show("Do you want to hide this column?", "Warning", MessageBoxButtons.YesNo) = DialogResult.No Then
                e.Handled = True ' Cancel the default action
            End If
        End If
    End If
End Sub