Back to Devexpress

GridGroupRowCommandContext Class

blazor-devexpress-dot-blazor-407dab60.md

latest7.5 KB
Original Source

GridGroupRowCommandContext Class

Contains contextual information for a Grid group row context menu.

Namespace : DevExpress.Blazor

Assembly : DevExpress.Blazor.v25.2.dll

NuGet Package : DevExpress.Blazor

Declaration

csharp
public class GridGroupRowCommandContext :
    GridCommandContextBase,
    IGridCommandContext,
    ICommandContextBase

Remarks

The DevExpress Blazor Grid allows you to display context menus with predefined and custom commands. Use the ContextMenus property to activate context menus for specific Grid elements.

Run Demo: Context Menu

Handle the CustomizeContextMenu event to modify the menu item collection. Use the Context event argument to identify the target Grid element and obtain contextual information.

Target ElementContext TypeContextual information
AnyGridCommandContextGrid
Data RowGridDataRowCommandContextGrid, Column, DataItem, RowVisibleIndex
FooterGridFooterCommandContextGrid, Column, SummaryItems
Group FooterGridGroupFooterCommandContextGrid, Column, GroupRowVisibleIndex, SummaryItems
Group PanelGridGroupPanelCommandContextGrid
Group RowGridGroupRowCommandContextGrid, GroupColumn, RowVisibleIndex
HeaderGridHeaderCommandContextGrid, Column

Example

The following example adds custom Expand Row / Collapse Row commands to the group row context menu:

razor
@inject WeatherForecastService ForecastService

<DxGrid Data="@forecasts"
        ShowGroupPanel="true"
        ContextMenus="GridContextMenus.GroupRow"
        CustomizeContextMenu="OnCustomizeContextMenu">
    <Columns>
        <DxGridSelectionColumn />
        <DxGridDataColumn FieldName=@nameof(WeatherForecast.TemperatureC) Caption="Temp. (C)" />
        <DxGridDataColumn FieldName=@nameof(WeatherForecast.TemperatureF) Caption="Temp. (F)" />
        <DxGridDataColumn FieldName=@nameof(WeatherForecast.Summary) Caption="Summary" GroupIndex="0" />
        <DxGridDataColumn FieldName=@nameof(WeatherForecast.Date) DisplayFormat="dd/MM/yyyy" />
    </Columns>
    <GroupSummary>
        <DxGridSummaryItem SummaryType="GridSummaryItemType.Count" FieldName="Date" FooterColumnName="Date" />
    </GroupSummary>
</DxGrid>

@code {
    private List<WeatherForecast>? forecasts;

    protected override async Task OnInitializedAsync() {
        forecasts = await ForecastService.GetForecastAsync();
    }
    void OnCustomizeContextMenu(GridCustomizeContextMenuEventArgs args) {
        if (args.Context is GridGroupRowCommandContext groupRowContext) {
            args.Items.AddCustomItem(0, "Collapse Group", () => {
                groupRowContext.Grid.BeginUpdate();
                groupRowContext.Grid.CollapseGroupRow(groupRowContext.RowVisibleIndex);
                groupRowContext.Grid.EndUpdate();
            });
            args.Items.AddCustomItem(0, "Expand Group", () => {
                groupRowContext.Grid.BeginUpdate();
                groupRowContext.Grid.ExpandGroupRow(groupRowContext.RowVisibleIndex);
                groupRowContext.Grid.EndUpdate();
            });
        }
    }
}
csharp
using System;

public class WeatherForecast {
    public int ID { get; set; }
    public DateTime Date { get; set; }
    public int TemperatureC { get; set; }
    public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
    public string? Summary { get; set; }
}
csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

public class WeatherForecastService {
    public readonly string[] Summaries = new[] {
        "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
    };
    private List<WeatherForecast>? Forecasts;
    public Task<List<WeatherForecast>> GetForecastAsync() {
        if (Forecasts == null) {
            var rnd = new Random();
            Forecasts = Enumerable.Range(1, 50).Select(index => new WeatherForecast {
                ID = index,
                Date = DateTime.Today.AddDays(index),
                TemperatureC = rnd.Next(-20, 55),
                Summary = Summaries[rnd.Next(Summaries.Length)]
            }).ToList();
        }
        return Task.FromResult(Forecasts);
    }
    public Task<List<WeatherForecast>> ChangeForecastAsync(WeatherForecast changed) {
        var originalForecast = Forecasts.FirstOrDefault(i => i.ID == changed.ID);
        if (originalForecast != null) {
            originalForecast.TemperatureC = changed.TemperatureC;
            originalForecast.Summary = changed.Summary;
            originalForecast.Date = changed.Date;
        } else {
            Forecasts.Add(changed);
        }
        return Task.FromResult(Forecasts);
    }
    public Task<List<WeatherForecast>> Remove(WeatherForecast forecast) {
        Forecasts.Remove(forecast);
        return Task.FromResult(Forecasts);
    }
}
csharp
// ...
builder.Services.AddSingleton<WeatherForecastService>();

Implements

IGridCommandContext

ICommandContextBase

Inheritance

Object DevExpress.Blazor.Grid.Internal.Base.ContextMenu.GridCommandContextBase GridGroupRowCommandContext

See Also

GridGroupRowCommandContext Members

DevExpress.Blazor Namespace