Skip to main content

Custom Summary

  • 2 minutes to read

Total and Group Summaries contain predefined aggregate functions. These functions allow you to calculate the following:

  • The number of data rows (Count)
  • The maximum and minimum values (Max and Min)
  • The sum and the average value (Sum and Average)

Handle the GridControl.CustomSummary event to apply custom rules to calculate summaries. This event allows you to implement custom aggregate functions, or use a custom algorithm to calculate summary values. Custom summaries allow you to:

  • Calculate summaries against records that meet specific criteria
  • Involve multiple data fields in calculations
  • Implement complex summary functions (for example, the standard deviation of a population), etc.

General Information

To calculate a summary manually:

  1. Create a summary item and set its SummaryItemBase.SummaryType property to SummaryItemType.Custom.
  2. Handle the GridControl.CustomSummary event and implement the summary calculation algorithm.

The GridControl calculates its summaries as follows:

Initialization
The GridControl raises the CustomSummary event and sets the CustomSummaryEventArgs.SummaryProcess property to Start. At this stage, you can initialize summary values (for example, reset internal counters).
Calculation
The GridControl raises the CustomSummary event multiple times, once for each data row. The CustomSummaryEventArgs.SummaryProcess property is set to Calculate. At this stage, you can calculate summaries.
Finalization
The GridControl raises the CustomSummary event and sets the CustomSummaryEventArgs.SummaryProcess property to Finalize. At this stage, you can assign the calculated summary to the event parameter’s TotalValue property.

To skip the Calculation stage and calculate a custom summary at the Initialization or Finalization stage, set the event parameter’s TotalValueReady property to true at the Initialization stage. This skips the Calculation stage and starts the Finalization stage.

How to Create Custom Summary

The following code sample shows how to use a custom summary to count the total value of selected rows:

WinUI Grid: Custom Summary

<dxg:GridControl x:Name="grid" 
                 ItemsSource="{x:Bind ViewModel.Source}" 
                 AutoGenerateColumns="True" 
                 SelectionMode="Row" 
                 ShowTotalSummary="True" 
                 CustomSummary="grid_CustomSummary" 
                 SelectionChanged="grid_SelectionChanged">
    <dxg:GridControl.TotalSummary>
        <dxg:GridTotalSummaryItem FieldName="UnitPrice" SummaryType="Custom"
                                  DisplayFormat="Total price (selected rows): {0}"/>
    </dxg:GridControl.TotalSummary>
</dxg:GridControl>
using DevExpress.Data;
using DevExpress.WinUI.Grid;
// ...

public sealed partial class MainWindow : Window {
    public MainViewModel ViewModel { get; } = new MainViewModel();
    public MainWindow() {
        this.InitializeComponent();
    }

    double sum;
    void grid_CustomSummary(object sender, CustomSummaryEventArgs e) {
        if(e.SummaryProcess == CustomSummaryProcess.Start) {
            sum = 0;
        } else if(e.SummaryProcess == CustomSummaryProcess.Calculate) {
            if(grid.IsRowSelected(e.RowHandle))
                sum += (double)e.FieldValue;
        } else {
            e.TotalValue = sum;
        }
    }

    void grid_SelectionChanged(object sender, GridSelectionChangedEventArgs e) {
        grid.UpdateTotalSummary();
    }
}