Back to Devexpress

GridView.PopupMenuShowing Event

windowsforms-devexpress-dot-xtragrid-dot-views-dot-grid-dot-gridview-fd5cb6bd.md

latest12.4 KB
Original Source

GridView.PopupMenuShowing Event

Allows you to customize built-in context menus or invoke custom menus.

Namespace : DevExpress.XtraGrid.Views.Grid

Assembly : DevExpress.XtraGrid.v25.2.dll

NuGet Packages : DevExpress.Win.Grid, DevExpress.Win.Navigation

Declaration

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

Event Data

The PopupMenuShowing event's data class is PopupMenuShowingEventArgs. The following properties provide information specific to this event:

PropertyDescription
AllowGets or sets whether to display the context menu. Inherited from BasePopupMenuShowingEventArgs.
HitInfoGets an object that identifies a clicked element.
MenuGets or sets the popup menu that is about to be displayed.
MenuTypeGets the type of the Grid View’s menu to be invoked.
PointGets or sets coordinates of the invoked context menu (top-left corner) relative to the parent control. Inherited from BasePopupMenuShowingEventArgs.
ScreenPointGets coordinates of the invoked context menu (top-left corner) relative to the screen. Inherited from BasePopupMenuShowingEventArgs.

The event data class exposes the following methods:

MethodDescription
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:

  1. Add a BarManager to the form and create a custom PopupMenu.

  2. Handle the GridView.PopupMenuShowing event and call the e.ShowCustomMenu method to display your custom menu instead of the default header menu.

csharp
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();
}
vb
Private Sub gridView1_PopupMenuShowing(ByVal sender As Object, ByVal e As PopupMenuShowingEventArgs)
    If e.MenuType = GridMenuType.Column Then
        popupMenu_Column.Tag = e.HitInfo
        popupMenu_Column.MenuCaption = $"{e.HitInfo.Column}"

        e.ShowCustomMenu(popupMenu_Column)
    End If
End Sub

Private Function GetHitInfo(ByVal link As BarItemLink) As GridHitInfo
    Dim menu As PopupMenu = TryCast(link.LinkedObject, PopupMenu)
    Return TryCast(menu.Tag, GridHitInfo)
End Function

Private Sub barButtonItem_Filter_ItemClick(ByVal sender As Object, ByVal e As ItemClickEventArgs)
    Dim info As GridHitInfo = GetHitInfo(e.Link)
    info.View.ShowFilterEditor(info.Column)
End Sub

Private Sub barButtonItem_ColumnChooser_ItemClick(ByVal sender As Object, ByVal e As ItemClickEventArgs)
    Dim info As GridHitInfo = GetHitInfo(e.Link)
    info.View.ShowCustomization()
End Sub

This example illustrates how to disable or remove unwanted conditions from the Automatic Filtering Row Menu for a “Postal Code” grid column.

csharp
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;
    }
}
vb
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraEditors.Controls

Private Sub GridView1_PopupMenuShowing(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs)
    Dim view As GridView = TryCast(sender, GridView)
    If e.MenuType = GridMenuType.AutoFilter AndAlso view.FocusedColumn = view.Columns("PostalCode") Then
        '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) IsNot Nothing Then
            e.Menu.Find(ColumnAutoFilterCondition.Contains).Enabled = False
        End If
        If e.Menu.Find(ColumnAutoFilterCondition.DoesNotContain) IsNot Nothing Then
            e.Menu.Find(ColumnAutoFilterCondition.DoesNotContain).Enabled = False
        End If
    End If
End Sub

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”).

csharp
// 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);

The following code snippets (auto-collected from DevExpress Examples) contain references 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.

winforms-grid-customize-footer-menu-calculate-custom-totals/CS/Form1.cs#L44

csharp
gridView1.OptionsView.ShowFooter = true;
    gridView1.PopupMenuShowing += new DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventHandler(gridView1_PopupMenuShowing);
}

connect-winforms-grid-to-webapi-service/CS/WinForms.Client/MainForm.cs#L18

csharp
this.colOrderDate.Summary.Add(item1);
    gridView1.PopupMenuShowing += GridView1_PopupMenuShowing;
}

connect-winforms-grid-to-backend-using-middletier-server/CS/WinForms.Client/MainForm.cs#L30

csharp
gridView.PopupMenuShowing += GridView_PopupMenuShowing;
gridView.EditFormShowing += GridView_EditFormShowing;

winforms-grid-customize-footer-menu-calculate-custom-totals/VB/Form1.vb#L39

vb
gridView1.OptionsView.ShowFooter = True
    AddHandler gridView1.PopupMenuShowing, New PopupMenuShowingEventHandler(AddressOf gridView1_PopupMenuShowing)
End Sub

connect-winforms-grid-to-backend-using-middletier-server/VB/WinForms.Client/MainForm.vb#L24

vb
colFirstName.Summary.Add(item1)
AddHandler gridView.PopupMenuShowing, AddressOf GridView_PopupMenuShowing
AddHandler gridView.EditFormShowing, AddressOf GridView_EditFormShowing

See Also

Popup and Context Menus

GridView Class

GridView Members

DevExpress.XtraGrid.Views.Grid Namespace