Back to Devexpress

Data Summaries in .NET MAUI Data Grid

maui-403471-data-grid-summaries.md

latest5.1 KB
Original Source

Data Summaries in .NET MAUI Data Grid

  • Jun 17, 2024
  • 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 SummaryAn aggregate function value is calculated over all rows in a group and displayed in a group row.Total SummaryAn 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:

FieldNameThe field name of the column for which the summary is calculated.TypeThe aggregate function used to calculate the summary value.DisplayFormat (optional)The summary value’s display format.

The grid supports five predefined summary aggregate functions:

CountThe number of data rows.Max and MinThe maximum and minimum values.Sum and AverageThe 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.

  • XAML

xaml
<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>
csharp
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

GroupSummaryTemplateDefines the template that defines the visual representation of group summary items.ExportGroupSummaryAppearanceDefines the appearance settings of group summaries in an exported document.

Total Summary

TotalSummaryAppearanceDefines the appearance settings that are applied to the current DataGridView’s total summary items.TotalSummaryTemplateDefines the template that defines the visual representation of total summary items.TotalSummaryHeightSpecifies the total summary panel height.ExportTotalSummaryAppearanceDefines the appearance settings of total summaries in an exported document.