Skip to main content

Tutorial: Total Summaries

  • 3 minutes to read

DevExpress Data Grid allows you to display total values — the number of records or the maximum and minimum values in a column. This tutorial describes how to the following:

  • Allow users to add or remove totals using built-in footer menus.
  • Prevent users from modifying totals.
  • Specify grid totals at design time or in code.

Watch Video: Total Summaries

Starting Point

Start with a GridControl that includes numeric data.

WinForms - Data Grid with Numeric Data, DevExpress

To enable total summary display and end-user interaction, show the View Footer:

  1. In the Properties window, expand the View’s GridView.OptionsView property.
  2. Activate the GridOptionsView.ShowFooter option.

    WinForms - Data Grid Show View Footer, DevExpress

gridView.OptionsView.ShowFooter = true;

End-User Capabilities

When the View’s footer is visible, a user can do the following:

  • Right-click the footer under a column to add a summary item:

    WinForms - Data Grid Add Summary in Context Menu, DevExpress

  • Right-click an existing total summary cell to add more summary items:

    WinForms - Data Grid Add a Second Summary in Context Menu, DevExpress

  • Right-click a summary to change the summary function:

    WinForms - Data Grid Change Summary Function in Context Menu, DevExpress

  • Right-click a summary item and select None in the context menu to hide the summary:

    WinForms - Data Grid Hide all Summary Items for a Column in Context Menu, DevExpress

  • Hide all summaries under a column:

    WinForms - Data Grid Hide a Specific Summary Item in Context Menu, DevExpress

Restrict End-User Capabilities

To prevent users from changing predefined summaries, disable the footer context menu:

  1. In the Properties window, expand the GridView.OptionsMenu property.

  2. Disable the GridOptionsMenu.EnableFooterMenu option.

    WinForms - Data Grid, DevExpress

gridView.OptionsMenu.EnableFooterMenu = false;

Create Summaries at Design Time

Use one of the following ways:

GridColumn.SummaryItem Property (Single Summary)

  1. In the Properties window, expand a column’s GridColumn.SummaryItem property.
  2. Set the GridSummaryItem.SummaryType property to the desired aggregate function.
  3. Specify the GridSummaryItem.DisplayFormat property to format the summary value. For example:

    WinForms - Data Grid, DevExpress

Tip

You can calculate a summary value based on values from another column. To do so, set the GridSummaryItem.FieldName property to the field name of the column you want to use for summary calculation.

Collection Editor (Multiple Summaries)

  1. In the Properties window, click the ellipsis button next to the GridColumn.Summary property:

    WinForms - Data Grid, DevExpress

  2. In the opened Collection Editor, specify SummaryType and DisplayFormat properties for the existing entry. Click Add to add new summary items. When the collection is ready, click OK to save changes.

    WinForms - Data Grid, DevExpress

Create Summaries in Code

Make sure you create summaries after the data source is assigned and the grid view is initialized (for example, on the Form.Load event).

Create a GridColumnSummaryItem object and add it to the GridColumn.Summary collection of the required column:

using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.Data;

    private void Form1_Load(object? sender, EventArgs e) {
        gridView.OptionsView.ShowFooter = true;

        GridColumnSummaryItem summary1 = new GridColumnSummaryItem(SummaryItemType.Max, "Quantity", "MAX Count={0}");
        gridView.Columns["Quantity"].Summary.Add(summary1);
        // You can also use the Summary collection's Add method directly
        // gridView.Columns["Quantity"].Summary.Add(SummaryItemType.Max, "Quantity", "MAX Count={0}");
}

The following image shows the result:

WinForms - Data Grid, DevExpress

See Also