Custom Summary

  • 5 minutes to read

Total summaries and group summaries provide five predefined aggregate functions. These functions allow you to calculate:

  • 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. When calculating a total summary value, the event is raised for each data row (or card in a Card View). When calculating a group summary value, this event fires for each data row (card) within a group. To enable you to perform any initialization and finalization, the event is also raised before and after processing rows.

The summary calculation consists of the three stages:

  • Initialization

    The CustomSummary event is raised once and the 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 in a View or group. 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 CustomSummaryEventArgs.TotalValue property.

To skip the Calculation stage and calculate a custom summary at the Initialization or Finalization stage, set the event parameter's 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 the specified grid column.

NOTE

A complete sample project is available at https://github.com/DevExpress-Examples/how-to-summarize-empty-cells-e948.

<Window x:Class="CustomSummary_EmptyCells.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
    xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
    Title="Window1" Height="350" Width="450" >
    <Grid>
        <dxg:GridControl x:Name="grid" CustomSummary="grid_CustomSummary" GroupCount="1">
            <dxg:GridControl.SortInfo>
                <dxg:GridSortInfo FieldName="Text" />
            </dxg:GridControl.SortInfo>
            <dxg:GridControl.Columns>
                <dxg:GridColumn FieldName="Text" />
                <dxg:GridColumn FieldName="Number" />
            </dxg:GridControl.Columns>
            <dxg:GridControl.View>
                <dxg:TableView x:Name="view" AutoWidth="True" 
                               NavigationStyle="Cell" ShowTotalSummary="True"/>
            </dxg:GridControl.View>
            <dxg:GridControl.TotalSummary>
                <dxg:GridSummaryItem FieldName="Number" SummaryType="Custom" 
                                     DisplayFormat="Total empty cells count: {0}"/>
            </dxg:GridControl.TotalSummary>
            <dxg:GridControl.GroupSummary>
                <dxg:GridSummaryItem FieldName="Number" SummaryType="Custom" 
                                     DisplayFormat="Group empty cells count: {0}"/>
            </dxg:GridControl.GroupSummary>
        </dxg:GridControl>
    </Grid>
</Window>
using System.Windows;
using System.Collections.Generic;
using DevExpress.Data;
using DevExpress.Xpf.Data;
using DevExpress.Xpf.Grid;
// ...

namespace CustomSummary_EmptyCells {
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window {
        List<TestData> list;
        Dictionary<int, bool> selectedValues = new Dictionary<int, bool>();

        public Window1() {
            InitializeComponent();
            list = new List<TestData>();
            for (int i = 0; i < 100; i++) {
                list.Add(new TestData() { Text = "group " + i % 10, Number = i });
                if (i % 3 == 0)
                    list[i].Number = null;
            }
            grid.ItemsSource = list;
        }

        int emptyCellsTotalCount = 0;
        int emptyCellsGroupCount = 0;

        void grid_CustomSummary(object sender, 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;
                }
            }
            if (e.IsGroupSummary) {
                if (e.SummaryProcess == CustomSummaryProcess.Start) {
                    emptyCellsGroupCount = 0;
                }
                if (e.SummaryProcess == CustomSummaryProcess.Calculate) {
                    int? val = (int?)e.FieldValue;
                    if (!val.HasValue)
                        emptyCellsGroupCount++;
                    e.TotalValue = emptyCellsGroupCount;
                }
            }
        }

        public class TestData {
            public string Text { get; set; }
            public int? Number { get; set; }
        }
    }
}