Back to Devexpress

Create Custom Summaries

aspnetbootstrap-118806-grid-view-data-summaries-create-custom-summaries.md

latest3.2 KB
Original Source

Create Custom Summaries

  • Nov 14, 2018
  • 2 minutes to read

Total summaries and group summaries provide five predefined aggregate functions. These functions allow you to calculate the number of rows, the maximum and minimum values, the sum and the average value. If you need to calculate a summary value using an aggregate function not included in the predefined set, set the summary item’s ASPxSummaryItemBase.SummaryType property to Custom and handle the Grid View control’s ASPxGridBase.CustomSummaryCalculate event.

The ASPxGridBase.CustomSummaryCalculate event fires for each row involved in summary calculation. Take into account the difference between calculating the total and group summary values:

  • When calculating a total summary value, the event is raised for each data row in the grid.
  • When calculating a group summary value, the event fires for each data row within the currently processed group.

Additionally, the event is raised before and after processing rows, which can be used to perform any initialization and finalization.

Example

This example shows how to implement a custom summary calculation.

  1. Create a summary item within the ASPxGridView.TotalSummary collection, and customize its settings, as shown below:

  2. Handle the ASPxGridBase.CustomSummaryCalculate event to perform custom calculations in the code-behind. This example demonstrates how to calculate a summary only for selected rows.

The image below shows the result:

csharp
int totalSum;
protected void grid_CustomSummaryCalculate(object sender, DevExpress.Data.CustomSummaryEventArgs e) {
    // Initialization.
    if (e.SummaryProcess == DevExpress.Data.CustomSummaryProcess.Start)
        totalSum = 0;
    // Calculation.
    if (e.SummaryProcess == DevExpress.Data.CustomSummaryProcess.Calculate)
        if (grid.Selection.IsRowSelectedByKey(e.GetValue(grid.KeyFieldName)))
            totalSum += Convert.ToInt32(e.FieldValue);
    // Finalization.
    if (e.SummaryProcess == DevExpress.Data.CustomSummaryProcess.Finalize)
        e.TotalValue = totalSum;
}
vb
Private totalSum As Integer
Protected Sub grid_CustomSummaryCalculate(ByVal sender As Object, ByVal e As DevExpress.Data.CustomSummaryEventArgs)
    ' Initialization.
    If e.SummaryProcess = DevExpress.Data.CustomSummaryProcess.Start Then
        totalSum = 0
    End If
    ' Calculation.
    If e.SummaryProcess = DevExpress.Data.CustomSummaryProcess.Calculate Then
        If grid.Selection.IsRowSelectedByKey(e.GetValue(grid.KeyFieldName)) Then
            totalSum += Convert.ToInt32(e.FieldValue)
        End If
    End If
    ' Finalization.
    If e.SummaryProcess = DevExpress.Data.CustomSummaryProcess.Finalize Then
        e.TotalValue = totalSum
    End If
End Sub