Data Summaries in .NET MAUI Data Grid
- 2 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, etc.
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 format.
The grid supports five predefined summary aggregate functions:
- Count—the number of data rows.
- Max and Min—the maximum and minimum values.
- Sum and Average—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.
Example
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;
// ...
void grid_CalculateCustomSummary(object sender, DevExpress.Data.CustomSummaryEventArgs e) {
if (e.FieldName.ToString () == "Shipped")
if (e.IsTotalSummary){
if (e.SummaryProcess == CustomSummaryProcess.Start) {
count = 0;
}
if (e.SummaryProcess == CustomSummaryProcess.Calculate) {
if (!(bool)e.FieldValue)
count++;
e.TotalValue = count;
}
}
}