Back to Devexpress

GridControl.CustomSummary Event

wpf-devexpress-dot-xpf-dot-grid-dot-gridcontrol-fa4012cd.md

latest8.9 KB
Original Source

GridControl.CustomSummary Event

Allows you to calculate a custom summary.

Namespace : DevExpress.Xpf.Grid

Assembly : DevExpress.Xpf.Grid.v25.2.dll

NuGet Package : DevExpress.Wpf.Grid.Core

Declaration

csharp
public event CustomSummaryEventHandler CustomSummary
vb
Public Event CustomSummary As CustomSummaryEventHandler

Event Data

The CustomSummary event's data class is CustomSummaryEventArgs. The following properties provide information specific to this event:

PropertyDescription
FieldValueGets the processed field value.
GroupLevelGets the nested level of the group whose summary value is being calculated.
GroupRowHandleGets a value identifying the group row whose child data rows are involved in summary calculation.
IsGroupSummaryGets whether a group summary value is being calculated.
IsTotalSummaryGets whether a total summary value is being calculated.
ItemGets a summary item whose value is being calculated.
ModeSpecifies how summaries are calculated - against all rows or for the selected rows.
RowGets the currently processed row.
RowHandleGets the handle of the processed row.
SummaryProcessGets a value indicating calculation stage.
TotalValueGets or sets the total summary value.
TotalValueReadyGets or sets whether the Calculation stage of the custom summary calculation process should be skipped.

The event data class exposes the following methods:

MethodDescription
GetGroupSummary(Int32, Object)Returns the value of the specified group summary for the specified group row.
GetValue(String)Returns the value in the specified field

Remarks

Total summaries and group summaries have predefined aggregate functions (COUNT, MAX, MIN, SUM, and AVG). To calculate custom summaries, handle the CustomSummary event. This event allows you to implement custom aggregate functions or use a custom algorithm to calculate summary values.

If the GridControl.View property is set to TreeListView, use the TreeListView.CustomSummary event.

Note

The CustomSummary event does not work in Server Mode.

Refer to the following help topic for more information: Custom Summary.

If you want to maintain a clean MVVM pattern and specify custom summaries in a View Model, create a command and bind it to the CustomSummaryCommand property.

Example

The following example demonstrates how to use custom summaries to count the total number of empty cells in the specified grid column:

View Example: How to Use Custom Summaries

xaml
<dxg:GridControl x:Name="grid"
                 CustomSummary="OnCustomSummary">
    <dxg:GridControl.Columns>
        <dxg:GridColumn FieldName="Text" GroupIndex="0" />
        <dxg:GridColumn FieldName="Number" />
    </dxg:GridControl.Columns>
    <dxg:GridControl.View>
        <dxg:TableView x:Name="view"
                       AutoWidth="True"
                       NavigationStyle="Cell"
                       TotalSummaryPosition="Bottom" />
    </dxg:GridControl.View>
    <dxg:GridControl.TotalSummary>
        <dxg:GridSummaryItem DisplayFormat="Total empty cells count: {0}"
                             FieldName="Number"
                             SummaryType="Custom" />
    </dxg:GridControl.TotalSummary>
    <dxg:GridControl.GroupSummary>
        <dxg:GridSummaryItem DisplayFormat="Group empty cells count: {0}"
                             FieldName="Number"
                             SummaryType="Custom" />
    </dxg:GridControl.GroupSummary>
</dxg:GridControl>
csharp
void OnCustomSummary(object sender, CustomSummaryEventArgs e) {
    if(((GridSummaryItem)e.Item).FieldName != "Number")
        return;
    if(e.SummaryProcess == CustomSummaryProcess.Start) {
        e.TotalValue = 0;
    }
    if(e.SummaryProcess == CustomSummaryProcess.Calculate) {
        if(IsEmptyCell(e.FieldValue))
            e.TotalValue = (int)e.TotalValue + 1;
    }
}
bool IsEmptyCell(object fieldValue) {
    return !((int?)fieldValue).HasValue;
}
vb
Private Sub OnCustomSummary(ByVal sender As Object, ByVal e As CustomSummaryEventArgs)
    If Not Equals(CType(e.Item, GridSummaryItem).FieldName, "Number") Then Return
    If e.SummaryProcess = CustomSummaryProcess.Start Then
        e.TotalValue = 0
    End If

    If e.SummaryProcess = CustomSummaryProcess.Calculate Then
        If IsEmptyCell(e.FieldValue) Then e.TotalValue = CInt(e.TotalValue) + 1
    End If
End Sub

Private Function IsEmptyCell(ByVal fieldValue As Object) As Boolean
    Return Not CType(fieldValue, Integer?).HasValue
End Function

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the CustomSummary event.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

wpf-data-grid-summarize-empty-cells/CS/CustomSummary_EmptyCells_CodeBehind/MainWindow.xaml#L10

xml
<dxg:GridControl x:Name="grid"
                 CustomSummary="OnCustomSummary">
    <dxg:GridControl.Columns>

wpf-data-grid-summarize-empty-cells/CS/CustomSummary_EmptyCells_CodeBehind/obj/Debug/net8.0-windows/MainWindow.g.cs#L106

csharp
#line 10 "..\..\..\MainWindow.xaml"
this.grid.CustomSummary += new DevExpress.Data.CustomSummaryEventHandler(this.OnCustomSummary);

wpf-data-grid-summarize-empty-cells/VB/CustomSummary_EmptyCells_CodeBehind/obj.NetFX/Debug/MainWindow.g.vb#L104

vb
#ExternalSource("..\..\MainWindow.xaml",10)
AddHandler Me.grid.CustomSummary, New DevExpress.Data.CustomSummaryEventHandler(AddressOf Me.OnCustomSummary)

See Also

Custom Summary

GridControl Class

GridControl Members

DevExpress.Xpf.Grid Namespace