Skip to main content
All docs
V17.2

Implementing Custom Behavior for Popup Menus

  • 4 minutes to read

This topic provides information about customization of popup menus. The base concepts for popup menus are described in the Popup Menus Overview topic.

Implementing Custom Behavior for Popup Menus

Basically, you can allow or prohibit the display of context menus by using the GridView.OptionsMenu property. For greater control over context menus, you can use the GridView.PopupMenuShowing event. If the GridView.OptionsMenu property allows menu invoking, the GridView.PopupMenuShowing event fires each time a user attempts to invoke a context menu.

Let’s consider the event GridView.PopupMenuShowing in detail. The data for this event is provided by the GridMenuEventArgs class. The event’s parameters enable you to identify the menu to be displayed, customize the menu or to stop it from being invoked.

The following example handles the GridView.PopupMenuShowing event to create a custom column header context menu. The event’s Menu parameter is used to access the current menu. The full code can be found in the Fixed Columns demo.


using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.Utils.Menu;
using DevExpress.XtraGrid.Menu;
// ...
private void gridView1_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e) {
    if(e.MenuType == DevExpress.XtraGrid.Views.Grid.GridMenuType.Column) {
        DevExpress.XtraGrid.Menu.GridViewColumnMenu menu = e.Menu as GridViewColumnMenu;
        //Erasing the default menu items
        menu.Items.Clear();
        if(menu.Column != null) {
            //Adding new items
            menu.Items.Add(CreateCheckItem("Fixed None", menu.Column, FixedStyle.None, 
              imageList2.Images[0]));
            menu.Items.Add(CreateCheckItem("Fixed Left", menu.Column, FixedStyle.Left, 
              imageList2.Images[1]));
            menu.Items.Add(CreateCheckItem("Fixed Right", menu.Column, FixedStyle.Right, 
              imageList2.Images[2]));
        }
    }
}

//Create a menu item
DXMenuCheckItem CreateCheckItem(string caption, GridColumn column, FixedStyle style, Image image) {
    DXMenuCheckItem item = new DXMenuCheckItem(caption, column.Fixed == style, 
      image, new EventHandler(OnFixedClick));
    item.Tag = new MenuInfo(column, style);
    return item;
}

//Menu item click handler
void OnFixedClick(object sender, EventArgs e) {
    DXMenuItem item = sender as DXMenuItem;
    MenuInfo info = item.Tag as MenuInfo;
    if(info == null) return;
    info.Column.Fixed = info.Style;
}

//The class that stores menu specific information
class MenuInfo {
    public MenuInfo(GridColumn column, FixedStyle style) {
        this.Column = column;
        this.Style = style;
    }
    public FixedStyle Style;
    public GridColumn Column;
}

ImplementCustomBehaviorofPopupMenus2

Additionally, you can stop a menu from being invoked via the event’s Allow parameter. The Point parameter gets or sets the context menu location.

The GridView.GridMenuItemClick event is fired in response to clicking a menu item. It enables you to cancel the default actions performed when clicking a particular item and/or provide your own item handling. The following example handles the GridView.GridMenuItemClick event to change the summary format in the Sub Total column. This is needed since default formatting is applied to summaries when one is changing their type using the context menu. Thus, the event handler below changes the format string applied to display values using the currency format.

The GridMenuItemClickEventArgs.MenuType and GridMenuItemClickEventArgs.DXMenuItem parameters are used to determine the menu item clicked . The GridMenuItemClickEventArgs.SummaryFormat parameter represents the current summary format.


using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Localization;
//...
private void bandedGridView1_GridMenuItemClick(object sender, GridMenuItemClickEventArgs e) {
    if(e.MenuType != GridMenuType.Summary || 
       e.DXMenuItem.Tag.Equals(GridStringId.MenuFooterCount)) return;
    if(e.Column.FieldName != "ID" && e.Column.FieldName != "Customer Name" && 
      e.Column.FieldName != "Year") {
        string sumFormat = e.SummaryFormat;
        int subSumFormat = sumFormat.IndexOf("{0");
        if(subSumFormat > 0) sumFormat = (e.DXMenuItem.Tag.Equals(GridStringId.MenuFooterSum) ? 
          "" : sumFormat.Substring(0, subSumFormat)) + "{0:c}";
        e.SummaryFormat = sumFormat;
    }
}

ImplementCustomBehaviorofPopupMenus

See Also