Description:
Is there a way to disable some of the default items that show on a column context menu? In particular, I would like to either disable or completely remove the "Runtime Column Customization" option.
Answer:
Yes, you can disable or hide menu items. Please use the ShowGridMenu event for this. Also, please note that starting from the 10.2 version, the GridView's ShowGridMenu event is marked obsolete, so it needs to use the PopupMenuShowing event.
Here is sample code:
C#using DevExpress.Utils.Menu;
using DevExpress.XtraGrid.Localization;
using DevExpress.XtraGrid.Views.Grid;
private DXMenuItem GetItemByStringId(DXPopupMenu menu, GridStringId id)
{
foreach ( DXMenuItem item in menu.Items )
if ( item.Caption == GridLocalizer.Active.GetLocalizedString(id) )
return item;
return null;
}
private void gridView1_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.GridMenuEventArgs e)
{
if ( e.MenuType == GridMenuType.Column )
{
// Customize
DXMenuItem miCustomize = GetItemByStringId(e.Menu, GridStringId.MenuColumnColumnCustomization);
if ( miCustomize != null )
miCustomize.Visible = false;
// Group By This Column
DXMenuItem miGroup = GetItemByStringId(e.Menu, GridStringId.MenuColumnGroup);
if ( miGroup != null )
miGroup.Enabled = false;
}
}
Visual BasicImports DevExpress.Utils.Menu
Imports DevExpress.XtraGrid.Localization
Imports DevExpress.XtraGrid.Views.Grid
Private Function GetItemByStringId(ByVal menu As DXPopupMenu, ByVal id As GridStringId) As DXMenuItem
For Each item As DXMenuItem In menu.Items
If item.Caption = GridLocalizer.Active.GetLocalizedString(id) Then
Return item
End If
Next item
Return Nothing
End Function
Private Sub gridView1_PopupMenuShowing(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Grid.GridMenuEventArgs)
If e.MenuType = GridMenuType.Column Then
' Customize
Dim miCustomize As DXMenuItem = GetItemByStringId(e.Menu, GridStringId.MenuColumnColumnCustomization)
If miCustomize IsNot Nothing Then
miCustomize.Visible = False
End If
' Group By This Column
Dim miGroup As DXMenuItem = GetItemByStringId(e.Menu, GridStringId.MenuColumnGroup)
If miGroup IsNot Nothing Then
miGroup.Enabled = False
End If
End If
End Sub
See Also:
How to add a new item to the grid's popup menu
How to show a context menu for grid rows
This code is very weak. These handlers will fail if another handler changed the caption before.
Hi Crono,
Yes, you are right. Since a menu item caption is changed in your code, you can search for a changed caption in menu items. Another approach is to use the Tag property. A GridStringId value corresponding to the menu item is set to the item Tag.
private DXMenuItem GetItemByStringId(DXPopupMenu menu, GridStringId id) { foreach (DXMenuItem item in menu.Items) { if ((GridStringId)item.Tag == id) { return item; } } return null; }
Hi,
it is polible to do this for DevExpress.Xpf.Grid with TableView?
Hello,
To process your inquiry in the most efficient manner, I've moved it to a separate thread and forwarded to the WPF team:
How to disable particular menu items in the default grid menus
Please refer to it for further correspondence.
in the newest version ShowGridMenu and PopupMenuShowing event do not exist anymore ?
How to do this in the newest version ?
Hello,
To process your recent post more efficiently, I created a separate ticket on your behalf: T350155: XtraGridControl version 15.2 - Cannot find the ShowGridMenu and PopupMenuShowing events. This ticket is currently in our processing queue. Our team will address it as soon as we have any updates.