Back to Devexpress

Custom Aggregate Functions

aspnet-119616-components-card-view-concepts-data-shaping-and-manipulation-data-summaries-custom-aggregate-functions.md

latest3.4 KB
Original Source

Custom Aggregate Functions

  • Jun 16, 2022
  • 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

  • Calculation

  • Finalization

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.

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