Skip to main content

Custom Summary

  • 3 minutes to read

Total summaries provide five 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 AVG).

If you want to calculate summaries using custom rules, handle the GridControl.CustomSummary event. This event enables you to implement custom aggregate functions, or calculate summary values, using a custom algorithm. Custom summaries allow you to:

  • calculate summaries against records that meet specific criteria
  • involve multiple data fields in calculations
  • implement complex summary functions (e.g. the standard deviation of a population), etc.

General Information

To calculate a summary manually:

The GridControl.CustomSummary event fires for each data row involved in summary calculation. The event is also raised before and after processing rows to enable you to perform any initialization and finalization.

The summary calculation consists of the following three stages:

  • Initialization

    The CustomSummary event is raised once and the DevExpress.Data.CustomSummaryEventArgs.SummaryProcess property is set to CustomSummaryProcess.Start. At this stage you can initialize summary values (e.g. reset internal counters).

  • Calculation

    The CustomSummary event occurs multiple times, once for each data row. The SummaryProcess property is set to CustomSummaryProcess.Calculate. At this stage you should accumulate summaries.

  • Finalization

    The CustomSummary event is raised once and the SummaryProcess property is set to CustomSummaryProcess.Finalize. At this point, calculate a final summary value and assign it to the event parameter’s DevExpress.Data.CustomSummaryEventArgs.TotalValue property.

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

Example

The following example demonstrates how to use custom summaries to count the total number of empty cells in a certain grid column.

<Page
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:CustomSummary"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:Grid="using:DevExpress.UI.Xaml.Grid"
    x:Class="CustomSummary.MainPage"
    mc:Ignorable="d">
    <Grid>
        <Grid:GridControl Name="grid"
                          AutoGenerateColumns="True"
                          ShowTotalSummary="True"
                          CustomSummary="grid_CustomSummary">
            <Grid:GridControl.TotalSummary>
                <Grid:GridSummaryItem FieldName="Number" SummaryType="Custom"
                                      DisplayFormat="Total empty cells count: {0}"/>
            </Grid:GridControl.TotalSummary>
        </Grid:GridControl>
    </Grid>
</Page>
using System.Collections.Generic;
using DevExpress.Data;
using DevExpress.UI.Xaml.Grid;
using Windows.UI.Xaml.Controls;

namespace CustomSummary {
    public sealed partial class MainPage : Page {

        int emptyCellsTotalCount = 0;

        private void grid_CustomSummary(object sender, DevExpress.Data.CustomSummaryEventArgs e) {
            if (((GridSummaryItem)e.Item).FieldName != "Number")
                return;
            if (e.IsTotalSummary) {
                if (e.SummaryProcess == CustomSummaryProcess.Start) {
                    emptyCellsTotalCount = 0;
                }
                if (e.SummaryProcess == CustomSummaryProcess.Calculate) {
                    int? val = (int?)e.FieldValue;
                    if (!val.HasValue)
                        emptyCellsTotalCount++;
                    e.TotalValue = emptyCellsTotalCount;
                }
            }
        }
    }
}