Custom Aggregate Functions

  • 2 minutes to read

Total summary provides five predefined aggregate functions. These functions allow you to calculate:

  • the number of cards
  • the maximum and minimum values
  • the sum and the average value

ASPxCardView includes the ASPxGridBase.CustomSummaryCalculate event, which enables you to implement custom aggregate functions or calculate summary values with a custom algorithm. Custom summaries allow you to do the following:

  • 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).

General Information

You can calculate a summary manually as follows:

The ASPxGridBase.CustomSummaryCalculate event fires for each card involved in summary calculation. The event is raised before and after card processing, so that you can initialize and finalize summary values as required.

Summary calculation consists of the following three stages.

  • Initialization

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

  • Calculation

    The CustomSummaryCalculate event fires multiple times, once for each card in a card view. The SummaryProcess property is set to CustomSummaryProcess.Calculate. At this stage you should accumulate summaries.

    The event parameter's properties allow the newly processed card and its values to be obtained.

  • Finalization

    The CustomSummaryCalculate event is raised once and the SummaryProcess property is set to 'CustomSummaryProcess.Finalize'. At this point, calculate the final summary value and assign it to the event parameter's TotalValue property.

To determine the current stage, use the event parameter's SummaryProcess property.

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

Example

The following example implements a custom summary calculation.

Note that in order to process selection changes on the server side, you can either set the ASPxGridBehaviorSettings.ProcessSelectionChangedOnServer property to true, or handle the ASPxClientCardView.SelectionChanged client event and call the client ASPxClientCardView.PerformCallback method.

int totalSum;
protected void ASPxCardView1_CustomSummaryCalculate(object sender, CustomSummaryEventArgs e)
{
    if (e.SummaryProcess == DevExpress.Data.CustomSummaryProcess.Start)
        totalSum = 0;
    // Calculation.
    if (e.SummaryProcess == DevExpress.Data.CustomSummaryProcess.Calculate)
        if (ASPxCardView1.Selection.IsCardSelectedByKey(e.GetValue(ASPxCardView1.KeyFieldName)))
            totalSum += Convert.ToInt32(e.FieldValue);
    // Finalization.
    if (e.SummaryProcess == DevExpress.Data.CustomSummaryProcess.Finalize)
        e.TotalValue = totalSum;
}