Skip to main content
All docs
V25.2
  • DxGrid.ContextMenus Property

    Specifies available context menus.

    Namespace: DevExpress.Blazor

    Assembly: DevExpress.Blazor.v25.2.dll

    Declaration

    [DefaultValue(GridContextMenus.None)]
    [Parameter]
    public GridContextMenus ContextMenus { get; set; }

    Property Value

    Type Default Description
    GridContextMenus None

    A collection of GridContextMenus values.

    Available values:

    Name Description
    None

    No context menus.

    Header

    The header context menu.

    Footer

    The footer context menu.

    DataRow

    The data row context menu.

    GroupPanel

    The group panel context menu.

    GroupRow

    The group row context menu.

    GroupFooter

    The group footer context menu.

    All

    All context menus.

    Remarks

    The DevExpress Blazor Grid allows you to display context menus with predefined and custom commands.

    DevExpress Blazor Grid - Context Menus

    Run Demo: Context Menu

    Add Context Menus

    You can display context menus for the following Grid elements:

    Blazor Grid Context Menu - Supported Regions: Data Row, Footer, Group Footer, Group Panel, Group Row, Header

    Assign GridContextMenus values to the ContextMenus property to display contextual commands when users right-click Grid elements.

    Note

    • The Grid does not display a context menu if the menu contains no items.
    • Once you activate a context menu for specific Grid elements, the browser context menu becomes unavailable for the corresponding region (even if the Grid context menu is empty).

    The following example demonstrates how to activate/deactivate Grid context menus:

    @inject WeatherForecastService ForecastService
    
    <DxGrid Data="@Data" ContextMenu="AvailableContextMenus">
        <Columns>
            <DxGridDataColumn FieldName="Date" DisplayFormat="D" />
            <DxGridDataColumn FieldName="TemperatureC" Caption="@("Temp. (\x2103)")" Width="120px" />
            <DxGridDataColumn FieldName="TemperatureF" Caption="@("Temp. (\x2109)")" Width="120px" />
            <DxGridDataColumn FieldName="Forecast" />
            <DxGridDataColumn FieldName="CloudCover" />
        </Columns>
    </DxGrid>
    
    @code {
        object Data { get; set; }
        GridContextMenus AvailableContextMenus;
    
        protected override void OnInitialized() {
            Data = ForecastService.GetForecast();
            // Activates all context menus (including menus without built-in items)
            AvailableContextMenus = GridContextMenus.All;
            // Disables all context menus
            AvailableContextMenus = GridContextMenus.None;
            // Activates only context menus that contain built-in items
            AvailableContextMenus = GridContextMenus.Header 
                                        | GridContextMenus.GroupPanel | GridContextMenus.GroupRow;
            // Activates menus for all elements except data rows
            AvailableContextMenus = GridContextMenus.All & ~GridContextMenus.DataRow;
        }
    }
    

    Built-in Items

    The table below lists context menu types and built-in commands available in the Grid:

    yes - The context menu includes this item by default.
    partially - You can add this item to the context menu.
    no - The context menu does not support this item.

    Menu Item Data Row Footer Group Footer Group Panel Group Row Header
    AutoFitAll partially partially partially partially partially yes
    ClearColumnSorting partially partially partially no no yes
    ClearGrouping partially partially partially yes partially partially
    CollapseAll partially partially partially yes yes partially
    ExpandAll partially partially partially yes yes partially
    GroupByColumn partially partially partially no no yes
    HideColumn partially partially partially no no yes
    HideGroupPanel partially partially partially yes partially yes
    ShowColumnChooser partially partially partially partially partially yes
    ShowFilterBuilder partially partially partially partially partially yes
    ShowGroupPanel partially partially partially partially partially yes
    SortColumnAscending partially partially partially no no yes
    SortColumnDescending partially partially partially no no yes
    UngroupColumn partially partially partially no no yes

    The resulting item collection depends on the menu type, Grid settings, and component state. For instance, a header context menu contains the SortColumnAscending command only if sorting is allowed (on both Grid and column levels). The ClearColumnSorting command is disabled if the target column is not sorted.

    Customization

    The CustomizeContextMenu event occurs before a context menu appears. Handle the event to customize the built-in item collection or add custom commands.

    The following code snippet customizes commands available in the header context menu:

    @inject NwindDataService NwindDataService
    
    <DxGrid Data="Data"
            ShowGroupPanel="true"
            ContextMenus="@(GridContextMenus.Header)"
            CustomizeContextMenu="CustomizeContextMenu">
        <Columns>
            <DxGridSelectionColumn />
            <DxGridDataColumn FieldName="City" />
            <DxGridDataColumn FieldName="Country" />
            <DxGridDataColumn FieldName="CustomerName" GroupIndex="0" />
            <DxGridDataColumn FieldName="ProductName" />
            <DxGridDataColumn FieldName="Total"
                              UnboundType="GridUnboundColumnType.Decimal"
                              UnboundExpression="[UnitPrice] * [Quantity]"
                              DisplayFormat="c"
                              MinWidth="100" />
        </Columns>
    </DxGrid>
    
    @code {
        BindingList<Invoice> Data { get; set; }
    
        void CustomizeContextMenu(GridCustomizeContextMenuEventArgs args) {
            if (args.Context is GridHeaderCommandContext headerContext) {
                // Customizes context menu commands for the Total column header
                if (headerContext.Column is IGridDataColumn dataColumn && dataColumn.FieldName == "Total") {
                    args.Items.Remove(GridContextMenuDefaultItemNames.GroupByColumn);
                    args.Items.Remove(GridContextMenuDefaultItemNames.UngroupColumn);
                }
                // Customizes context menu commands for the selection column header
                if (headerContext.Column is IGridSelectionColumn selectionColumn) {
                    var isFixed = selectionColumn.FixedPosition != GridColumnFixedPosition.None;
                    string itemText = isFixed ? "Unfix Column" : "Fix Column to the Left";
                    var newValue = isFixed ? GridColumnFixedPosition.None : GridColumnFixedPosition.Left;
    
                    args.Items.AddCustomItem(itemText, () => {
                        headerContext.Grid.BeginUpdate();
                        headerContext.Column.FixedPosition = newValue;
                        headerContext.Grid.EndUpdate();
                    });
                }
            }
        }
    
        protected override async Task OnInitializedAsync() {
            var invoices = await NwindDataService.GetInvoicesAsync();
            Data = new BindingList<Invoice>(invoices.ToList());
        }
    }
    

    Blazor Grid - Customize Context Menu

    Implements

    See Also