Skip to main content

DxGrid.CustomSummary Event

Allows you to create custom summary items.

Namespace: DevExpress.Blazor

Assembly: DevExpress.Blazor.v21.2.dll

Declaration

[Parameter]
public Action<GridCustomSummaryEventArgs> CustomSummary { get; set; }

Parameters

Type Description
GridCustomSummaryEventArgs

A GridCustomSummaryEventArgs object that contains data for this event.

Remarks

Handle the CustomSummary event to compute total or group summaries based on custom logic. For more information, refer to the following topic: Add a Custom Summary Item.

Use GridCustomSummaryEventArgs (TotalValue, FieldValue, and so on) to evaluate the summary value and to access other grid data.

Note

Custom summary calculation is limited when you use a Server Mode data source. The CustomSummary event fires only once, when the SummaryStage event argument is set to Finalize.

The Grid does not support custom summary calculation when you use a GridDevExtremeDataSource.

The example below illustrates how to create a custom summary item, whose value is evaluated and formatted within the CustomSummary and CustomizeSummaryDisplayText event handlers.

@using Microsoft.EntityFrameworkCore
@inject IDbContextFactory<NorthwindContext> NorthwindContextFactory
@implements IDisposable

<DxGrid Data="GridDataSource"
        UnboundColumnData="Grid_CustomUnboundColumnData"
        CustomizeSummaryDisplayText="Grid_CustomizeSummaryDisplayText"
        CustomSummary="Grid_CustomSummary"
        AllowSelectRowByClick="true"
        SelectedDataItems="@SelectedDataItems"
        SelectedDataItemsChanged="Grid_SelectedDataItemsChanged"
        @ref="Grid">
    <Columns>
        <DxGridSelectionColumn />
        <DxGridDataColumn FieldName="ProductId" Caption="Product ID" DisplayFormat="d" />
        <DxGridDataColumn FieldName="UnitPrice" />
        <DxGridDataColumn FieldName="Quantity" />
        <DxGridDataColumn FieldName="Discount" DisplayFormat="p0" />
        <DxGridDataColumn FieldName="TotalPrice"
                          DisplayFormat="c"
                          UnboundType="GridUnboundColumnType.Decimal" />
    </Columns>
    <TotalSummary>
        <DxGridSummaryItem SummaryType="GridSummaryItemType.Custom" Name="Custom" FieldName="TotalPrice" />
    </TotalSummary>
</DxGrid>

@code {
    IEnumerable<object> GridDataSource { get; set; }
    NorthwindContext Northwind { get; set; }
    IReadOnlyList<object> SelectedDataItems { get; set; }
    IGrid Grid { get; set; }

    protected override void OnInitialized() {
        Northwind = NorthwindContextFactory.CreateDbContext();
        GridDataSource = Northwind.OrderDetails
            .Include(i => i.Order)
            .Include(i => i.Product)
            .ToList();
        SelectedDataItems = GridDataSource.Skip(1).Take(2).ToList();
    }

    void Grid_CustomSummary(GridCustomSummaryEventArgs e) {
        switch (e.SummaryStage) {
            case GridCustomSummaryStage.Start:
                e.TotalValue = 0m;
                break;
            case GridCustomSummaryStage.Calculate:
                if (e.Grid.IsDataItemSelected(e.DataItem))
                    e.TotalValue = (decimal)e.TotalValue + (decimal)e.GetRowValue("TotalPrice");
                break;
            case GridCustomSummaryStage.Finalize:
                e.TotalValueReady = true;
                break;
        }
    }

    void Grid_CustomizeSummaryDisplayText(GridCustomizeSummaryDisplayTextEventArgs e) {
        if (e.Item.Name == "Custom")
            e.DisplayText = string.Format("Sum of Selected: {0:c}", e.Value);
    }

    void Grid_CustomUnboundColumnData(GridUnboundColumnDataEventArgs e) {
        if (e.FieldName == "TotalPrice") {
            var UnitPrice = Convert.ToDecimal(e.GetRowValue("UnitPrice"));
            var Quantity = Convert.ToDecimal(e.GetRowValue("Quantity"));
            var Discount = Convert.ToDecimal(e.GetRowValue("Discount"));
            e.Value = Quantity * UnitPrice * (1 - Discount);
        }
    }

    void Grid_SelectedDataItemsChanged() {
        Grid.RefreshSummary();
    }

    public void Dispose() {
        Northwind?.Dispose();
    }
}

DevExpress Blazor Grid - Custom Summary

Run Demo: Grid - Custom Summary

See Also