Data Summaries in .NET MAUI Data Grid
- 3 minutes to read
DataGridView can automatically calculate summaries for groups of rows or entire columns. For example, it can sum values, count the number of records, detect a minimum or maximum value, and so on.
The grid supports two summary types:
- Group Summary
- An aggregate function value is calculated over all rows in a group and displayed in a group row.
- Total Summary
- An aggregate function value is calculated over all rows in a column and displayed below this column.
To create a total or group summary, add a GridColumnSummary object to the DataGridView.TotalSummaries or DataGridView.GroupSummaries collection, and adjust the following summary settings:
- FieldName
- The field name of the column for which the summary is calculated.
- Type
- The aggregate function used to calculate the summary value.
- DisplayFormat (optional)
- The summary value’s display format.
The grid supports five predefined summary aggregate functions:
Count
- The number of data rows.
Max
andMin
- The maximum and minimum values.
Sum
andAverage
- The sum and average values.
You can also implement and apply a custom aggregate function. To do this, set the GridColumnSummary.Type property to Custom, and handle the DataGridView.CustomSummary event.
The following example uses predefined aggregate functions (Max and Sum) and a custom rule to calculate group and total summaries for a grid that displays orders grouped by dates.
Set up the following summaries:
- A group summary to display the maximum Total value for each group of orders.
- A total summary to calculate the sum of values in the Total column.
- A custom total summary to count the number of orders with the false value in the Shipped column.
<dxg:DataGridView x:Name="grid" ItemsSource="{Binding Orders}"
CustomSummary="grid_CalculateCustomSummary">
<!-- ... -->
<dxg:DataGridView.GroupSummaries>
<dxg:GridColumnSummary FieldName="Total" Type="Max"/>
</dxg:DataGridView.GroupSummaries>
<dxg:DataGridView.TotalSummaries>
<dxg:GridColumnSummary FieldName="Total" Type="Sum"
DisplayFormat="Total: {0:C0}"/>
<dxg:GridColumnSummary FieldName="Shipped" Type="Custom"
DisplayFormat="Not Shipped: {0}"/>
</dxg:DataGridView.TotalSummaries>
</dxg:DataGridView>
int count;
// ...
private void grid_CustomSummary(object sender, DevExpress.Maui.DataGrid.CustomSummaryEventArgs e) {
if (e.FieldName.ToString() == "Shipped")
if (e.IsTotalSummary) {
if (e.SummaryProcess == DevExpress.Maui.Core.DataSummaryProcess.Start) {
count = 0;
}
if (e.SummaryProcess == DevExpress.Maui.Core.DataSummaryProcess.Calculate) {
if (!(bool)e.Value)
count++;
e.TotalValue = count;
}
}
}
Use the TotalSummaryVisibility property to manage the total summary visibility.
Specify Summary Appearance
This section contains API members that allow you to customize summary appearance:
Group Summary
- GroupSummaryTemplate
- Defines the template that defines the visual representation of group summary items.
- ExportGroupSummaryAppearance
- Defines the appearance settings of group summaries in an exported document.
Total Summary
- TotalSummaryAppearance
- Defines the appearance settings that are applied to the current DataGridView’s total summary items.
- TotalSummaryTemplate
- Defines the template that defines the visual representation of total summary items.
- TotalSummaryHeight
- Specifies the total summary panel height.
- ExportTotalSummaryAppearance
- Defines the appearance settings of total summaries in an exported document.