aspnetbootstrap-118806-grid-view-data-summaries-create-custom-summaries.md
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:
Additionally, the event is raised before and after processing rows, which can be used to perform any initialization and finalization.
This example shows how to implement a custom summary calculation.
Create a summary item within the ASPxGridView.TotalSummary collection, and customize its settings, as shown below:
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:
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;
}
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