Back to Devexpress

How to: Create Group Summaries

windowsforms-5522-controls-and-libraries-data-grid-examples-summaries-how-to-create-group-summaries.md

latest2.8 KB
Original Source

How to: Create Group Summaries

  • Nov 13, 2018
  • 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:

csharp
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);
}
vb
Imports DevExpress.XtraGrid
Imports DevExpress.XtraGrid.Views.Grid

Public Sub New()
    InitializeComponent()
    AddHandler Me.Load, AddressOf OnFormLoad
End Sub

Private Sub ExcelFiltering_Load(ByVal sender As Object, ByVal e As EventArgs)
    bandedGridView1.Columns("Discontinued").Group()
    CreateGroupSummaries()
End Sub

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);
}