Skip to main content

Custom Summary

  • 3 minutes to read

Total summaries 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 or use the GridControl.CustomSummaryCommand property to apply custom rules to calculate summaries. 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.

If the GridControl.View property is set to TreeListView, use the TreeListView.CustomSummary event or the TreeListView.CustomSummaryCommand property.

General Information

To calculate a summary manually:

  1. Create a summary item and set its SummaryItemBase.SummaryType property to SummaryItemType.Custom.
  2. Create a command that uses a custom algorithm to calculate summary values.
  3. Bind this command to the GridControl.CustomSummaryCommand property.

The GridControl calculates its summaries as follows:

Initialization
The GridControl executes the CustomSummary command and sets the SummaryArgs.SummaryProcess property to Start. At this stage, you can initialize summary values (for example, reset internal counters).
Calculation
The GridControl executes the CustomSummary command multiple times, once for each data row in a View or group. The SummaryArgs.SummaryProcess property is set to Calculate. At this stage, you can calculate summaries.
Finalization
The GridControl executes the CustomSummary command and sets the SummaryArgs.SummaryProcess property to Finalize. At this stage, you can assign the calculated summary to the SummaryArgs.TotalValue property.

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

Example

The following code sample calculates the total number of empty cells in the specified column:

DevExpress WPF | Grid Control - Custom Summaries

View Example: How to Use Custom Summaries

<dxg:GridControl ItemsSource="{Binding Items}"
                 CustomSummaryCommand="{Binding CustomSummaryCommand}">
    <dxg:GridControl.Columns>
        <dxg:GridColumn FieldName="Text" GroupIndex="0" />
        <dxg:GridColumn FieldName="Number" />
    </dxg:GridControl.Columns>
    <dxg:GridControl.View>
        <dxg:TableView AutoWidth="True"
                       NavigationStyle="Cell"
                       ShowTotalSummary="True" />
    </dxg:GridControl.View>
    <dxg:GridControl.TotalSummary>
        <dxg:GridSummaryItem DisplayFormat="Total empty cells count: {0}"
                             FieldName="Number"
                             SummaryType="Custom" />
    </dxg:GridControl.TotalSummary>
    <dxg:GridControl.GroupSummary>
        <dxg:GridSummaryItem DisplayFormat="Group empty cells count: {0}"
                             FieldName="Number"
                             SummaryType="Custom" />
    </dxg:GridControl.GroupSummary>
</dxg:GridControl>
using DevExpress.Mvvm;
using DevExpress.Mvvm.DataAnnotations;
using DevExpress.Mvvm.Xpf;
// ...
public class MainViewModel : ViewModelBase {
// ...
    [Command]
    public void CustomSummary(RowSummaryArgs args) {
        if(args.SummaryItem.PropertyName != "Number")
            return;
        if(args.SummaryProcess == SummaryProcess.Start) {
            args.TotalValue = 0;
        } 
        if(args.SummaryProcess == SummaryProcess.Calculate) {
            if(IsEmptyCell(args.FieldValue))
                args.TotalValue = (int)args.TotalValue + 1;
        }
    }
    bool IsEmptyCell(object fieldValue) {
        return !((int?)fieldValue).HasValue;
    }
}