Custom Summary

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.

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 {
        List<TestData> list;
        Dictionary<int, bool> selectedValues = new Dictionary<int, bool>();

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

        int emptyCellsTotalCount = 0;



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

        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;
                }
            }

        }
    }
}