windowsforms-114633-controls-and-libraries-data-grid-getting-started-walkthroughs-summaries-tutorial-custom-summary-functions.md
This walkthrough is a transcript of the Custom Summary Functions video available on the DevExpress YouTube Channel.
The DevExpress GridControl allows you to implement custom aggregate functions to calculate total and group summaries. In this tutorial, you will create a total summary that is calculated using values from two different fields. You will also implement a custom group summary to calculate the number of records that have Discontinued a field value set to true.
Start with a GridControl that doesn’t have any summaries.
First, enable the View footer. Expand the GridView.OptionsView property and turn on the GridOptionsView.ShowFooter option.
After that, select the Unit Price column, expand its GridColumn.SummaryItem property and set GridSummaryItem.SummaryType to SummaryItemType.Custom. Customize text formatting using the GridSummaryItem.DisplayFormat property. Additionally, set the GridSummaryItem.Tag property to 1. This value will be used to identify this summary item in code.
Now create a new group summary. Invoke the Grid Designer, switch to the Group Summary Items Page and add a new item. Specify text formatting, set the GridSummaryItem.FieldName property to Discontinued , the GridSummaryItem.SummaryType property to SummaryItemType.Custom and the GridSummaryItem.Tag property to 2.
Go to the code view and declare two variables that will store summary values.
// Variables that store summary values.
int discontinuedProductsCount;
double totalPrice;
To implement the summary calculation algorithm, handle the View’s GridView.CustomSummaryCalculate event. In the event handler, use the GridSummaryItem.Tag property to identify summary items.
private void gridView_CustomSummaryCalculate(object sender, CustomSummaryEventArgs e) {
GridView view = sender as GridView;
// Get the summary ID.
int summaryID = Convert.ToInt32((e.Item as GridSummaryItem).Tag);
}
Initialization
Calculation
Finalization
Run the application to see the result. The View’s footer cell under the Unit Price column displays the summary calculated against two fields at once. Group the grid’s data by the Category column. Group rows display the number of discontinued products in each group.
// Variables that store summary values.
int discontinuedProductsCount;
double totalPrice;
private void gridView_CustomSummaryCalculate(object sender, CustomSummaryEventArgs e) {
GridView view = sender as GridView;
// Get the summary ID.
int summaryID = Convert.ToInt32((e.Item as GridSummaryItem).Tag);
// Initialization.
if (e.SummaryProcess == CustomSummaryProcess.Start) {
discontinuedProductsCount = 0;
totalPrice = 0;
}
// Calculation.
if (e.SummaryProcess == CustomSummaryProcess.Calculate) {
switch (summaryID) {
case 1: // The total summary calculated against the 'UnitPrice' column.
int unitsInStock = Convert.ToInt32(view.GetRowCellValue(e.RowHandle, "UnitsInStock"));
totalPrice += Convert.ToDouble(e.FieldValue) * unitsInStock;
break;
case 2: // The group summary.
Boolean isDiscontinued = Convert.ToBoolean(e.FieldValue);
if (isDiscontinued) discontinuedProductsCount++;
break;
}
}
// Finalization.
if (e.SummaryProcess == CustomSummaryProcess.Finalize) {
switch (summaryID) {
case 1:
e.TotalValue = totalPrice;
break;
case 2:
e.TotalValue = discontinuedProductsCount;
break;
}
}
}
See Also
Working with Total, Group, and Custom Summaries in Code