This example shows how to calculate group summaries and display them within group rows residing at the top grouping level. A group summary represents a value of the aggregate function calculated over all data rows within a group. Group summary items are stored within the grid's GroupSummary collection.
<Window x:Class="DXGrid_DisplayGroupSummaries.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
Title="Window1" Height="300" Width="458">
<Grid>
<dxg:GridControl x:Name="grid" AutoGenerateColumns="AddNew"
CustomSummaryExists="grid_CustomSummaryExists">
<dxg:GridControl.View>
<dxg:TableView AutoWidth="True"/>
</dxg:GridControl.View>
<dxg:GridControl.GroupSummary>
<dxg:GridSummaryItem FieldName="Age" SummaryType="Min"/>
<dxg:GridSummaryItem FieldName="Age" SummaryType="Max"/>
</dxg:GridControl.GroupSummary>
</dxg:GridControl>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Documents;
using DevExpress.Data;
using DevExpress.Xpf.Grid;
namespace DXGrid_DisplayGroupSummaries {
public partial class Window1 : Window {
public Window1() {
InitializeComponent();
grid.ItemsSource = new AccountList().GetData();
grid.GroupBy(grid.Columns["Married"], ColumnSortOrder.Ascending);
}
private void CreateGroupSummaries() {
grid.GroupSummary.Add(new GridSummaryItem() {
FieldName = "Age",
SummaryType = SummaryItemType.Min
});
grid.GroupSummary.Add(new GridSummaryItem() {
FieldName = "Age",
SummaryType = SummaryItemType.Max
});
}
private void grid_CustomSummaryExists(object sender, CustomSummaryExistEventArgs e) {
e.Exists = e.GroupLevel == 0;
}
}
public class AccountList {
public List<Account> GetData() {
return CreateAccounts();
}
private List<Account> CreateAccounts() {
List<Account> list = new List<Account>();
list.Add(new Account() {
UserName = "Nick White",
RegistrationDate = DateTime.Today,
Married = true,
Age = 42
});
list.Add(new Account() {
UserName = "Jack Rodman",
RegistrationDate = new DateTime(2009, 5, 10),
Married = false,
Age = 30
});
list.Add(new Account() {
UserName = "Sandra Sherry",
RegistrationDate = new DateTime(2008, 12, 22),
Married = false,
Age = 18
});
list.Add(new Account() {
UserName = "Sabrina Vilk",
RegistrationDate = DateTime.Today,
Married = true,
Age = 24
});
list.Add(new Account() {
UserName = "Mike Pearson",
RegistrationDate = new DateTime(2008, 10, 18),
Married = true,
Age = 37
});
return list;
}
}
public class Account {
public string UserName { get; set; }
public DateTime RegistrationDate { get; set; }
public bool Married { get; set; }
public int Age { get; set; }
}
}
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Windows
Imports System.Windows.Documents
Imports DevExpress.Data
Imports DevExpress.Xpf.Grid
Namespace DXGrid_DisplayGroupSummaries
Partial Public Class Window1
Inherits Window
Public Sub New()
InitializeComponent()
grid.ItemsSource = New AccountList().GetData()
grid.GroupBy(grid.Columns("Married"), ColumnSortOrder.Ascending)
End Sub
Private Sub CreateGroupSummaries()
grid.GroupSummary.Add(New GridSummaryItem() With {.FieldName = "Age", .SummaryType = SummaryItemType.Min})
grid.GroupSummary.Add(New GridSummaryItem() With {.FieldName = "Age", .SummaryType = SummaryItemType.Max})
End Sub
Private Sub grid_CustomSummaryExists(ByVal sender As Object, ByVal e As CustomSummaryExistEventArgs)
e.Exists = e.GroupLevel = 0
End Sub
End Class
Public Class AccountList
Public Function GetData() As List(Of Account)
Return CreateAccounts()
End Function
Private Function CreateAccounts() As List(Of Account)
Dim list As New List(Of Account)()
list.Add(New Account() With {.UserName = "Nick White", .RegistrationDate = DateTime.Today, .Married = True, .Age = 42})
list.Add(New Account() With {.UserName = "Jack Rodman", .RegistrationDate = New DateTime(2009, 5, 10), .Married = False, .Age = 30})
list.Add(New Account() With {.UserName = "Sandra Sherry", .RegistrationDate = New DateTime(2008, 12, 22), .Married = False, .Age = 18})
list.Add(New Account() With {.UserName = "Sabrina Vilk", .RegistrationDate = DateTime.Today, .Married = True, .Age = 24})
list.Add(New Account() With {.UserName = "Mike Pearson", .RegistrationDate = New DateTime(2008, 10, 18), .Married = True, .Age = 37})
Return list
End Function
End Class
Public Class Account
Private privateUserName As String
Public Property UserName() As String
Get
Return privateUserName
End Get
Set(ByVal value As String)
privateUserName = value
End Set
End Property
Private privateRegistrationDate As DateTime
Public Property RegistrationDate() As DateTime
Get
Return privateRegistrationDate
End Get
Set(ByVal value As DateTime)
privateRegistrationDate = value
End Set
End Property
Private privateMarried As Boolean
Public Property Married() As Boolean
Get
Return privateMarried
End Get
Set(ByVal value As Boolean)
privateMarried = value
End Set
End Property
Private privateAge As Integer
Public Property Age() As Integer
Get
Return privateAge
End Get
Set(ByVal value As Integer)
privateAge = value
End Set
End Property
End Class
End Namespace