Skip to main content

How to: Create Group Summaries

  • 2 minutes to read

The following example shows how to create two group summary items. The first summary item will represent the number of records within groups, and will be displayed in group rows. The second item will calculate the sum of values against the UnitPrice field, and will be displayed under the Unit Price column within group footers. The result of the code execution is presented below:

Summary-GroupCodeResult

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

public MyForm() {
    InitializeComponent();
    this.Load += OnFormLoad;
}

private void OnFormLoad(object sender, EventArgs e) {
    bandedGridView1.Columns["Discontinued"].Group();
    CreateGroupSummaries();
}

private void CreateGroupSummaries() {
    // Make the group footers always visible.
    bandedGridView1.OptionsView.GroupFooterShowMode = GroupFooterShowMode.VisibleAlways;
    // Create and setup the first summary item.
    GridGroupSummaryItem item = new GridGroupSummaryItem();
    item.FieldName = "ProductName";
    item.SummaryType = DevExpress.Data.SummaryItemType.Count;
    bandedGridView1.GroupSummary.Add(item);
    // Create and setup the second summary item.
    GridGroupSummaryItem item1 = new GridGroupSummaryItem();
    item1.FieldName = "UnitPrice";
    item1.SummaryType = DevExpress.Data.SummaryItemType.Sum;
    item1.DisplayFormat = "Total {0:c2}";
    item1.ShowInGroupColumnFooter = bandedGridView1.Columns["UnitPrice"];
    bandedGridView1.GroupSummary.Add(item1);
}