Skip to main content

Tutorial: Obtain Summary Values

  • 3 minutes to read

This walkthrough is a transcript of the Obtain Summary Values video available on the DevExpress YouTube Channel.

The GridControl allows you to obtain summary values so you can use them in calculations or to customize grid Views. In this tutorial, you’ll first see how you can format group rows based on their group summary values. Then, you will see how to obtain total summary values and use them to specify a different display text for footer cells.

GridView_Summaries_ObtainTotalSummaryValueResult

Starting Point

Start with a GridControl that already has total and group summaries. A footer cell under the Order Sum column displays the average column value. The Unit Price column has two summaries that calculate the maximum and minimum column values. Group rows display the number of records in each group.

GridView_Summaries_InitialGridForObtainSummary

Obtaining Group Summary Values

Close the application. Select the View and handle its GridView.RowStyle event to specify the group row background color depending on group summary values. In the event handler, call the GridView.GetGroupSummaryValues method. This method returns the hash table containing values of all summaries for a specified group row. Since the grid only displays one group summary, simply obtain the first element from the View’s GridView.GroupSummary collection. Use this value as the key value for the hash table to obtain the summary item value. If the obtained value is greater than 3, change the background color.

using DevExpress.XtraGrid.Views.Grid;
// ...
private void gridView_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e) {
    GridView view = sender as GridView;
    if (view == null) return;
    if (e.RowHandle >= 0 || view.GroupCount == 0) return;
    // Get all group summary values.
    Hashtable ht = view.GetGroupSummaryValues(e.RowHandle);
    // Access the first group summary.
    GridSummaryItem groupSummaryItem = view.GroupSummary[0];
    // Obtain the group summary's value.
    int childRecordCount = Convert.ToInt32(ht[groupSummaryItem]);
    if (childRecordCount > 3)
        e.Appearance.BackColor = Color.FromArgb(70, Color.Red);
}

Run the application to see the result. Group rows whose summary values are greater than 3 are highlighted now.

GridView_Summaries_ObtainGroupSummaryValueResult

Obtaining Total Summary Values

The following example demonstrates how to obtain a total summary value:

var summaryValue = gridColumn.SummaryItem.SummaryValue;

Grid columns can display multiple summaries in the Grid’s footer. The following example demonstrates how to obtain all summaries:

List<object> summaryValues = new List<object>();
foreach(DevExpress.XtraGrid.GridSummaryItem summary in gridColumn2.Summary)
    summaryValues.Add(summary.SummaryValue);

Handle the GridView.CustomDrawFooterCell event to custom format the total summary display text, customize its appearance settings, etc.

First, get the summary item being processed using event arguments. Then, obtain the summary value using the GridSummaryItem.SummaryValue property. Finally, generate a new display text, which includes the summary type, column caption and summary value, and assign this text to the event’s parameter. The default painting mechanism will be invoked after event handler execution, and it will automatically display the modified text.

private void gridView_CustomDrawFooterCell(object sender, DevExpress.XtraGrid.Views.Grid.FooterCellCustomDrawEventArgs e) {
    GridSummaryItem summary = e.Info.SummaryItem;
    // Obtain the total summary's value.
    double summaryValue = Convert.ToDouble(summary.SummaryValue);
    string summaryText = String.Format("{0} {1} = {2:c2}", summary.SummaryType, e.Column.GetCaption(), summaryValue);
    e.Info.DisplayText = summaryText;
}

Run the application to see the result. All total summaries display the summary type, and target the column caption and formatted value.

GridView_Summaries_ObtainTotalSummaryValueResult

See Also