Back to Devexpress

How To: Calculate Custom Summaries Based on Column Summary Items

windowsforms-117174-controls-and-libraries-data-grid-examples-summaries-how-to-calculate-custom-summaries-based-on-column-summary-items.md

latest7.4 KB
Original Source

How To: Calculate Custom Summaries Based on Column Summary Items

  • Nov 13, 2018
  • 3 minutes to read

The example below creates a new custom summary items and populates it on the GridView.CustomSummaryCalculate event. You can test this sample in our DevExpress Demo Center (the link requires Demo Center of version 16.2 or newer to be installed).

The custom summary item is located under the “Length” column. To add it, set up the GridColumn.SummaryItem property.

csharp
gridView.OptionsView.ShowFooter = true;
GridColumn column = gridView.Columns["Length"];
column.SummaryItem.SummaryType = SummaryItemType.Custom;
vb
gridView.OptionsView.ShowFooter = True
Dim column As GridColumn = gridView.Columns("Length")
column.SummaryItem.SummaryType = SummaryItemType.Custom

The CustomSummaryCalculate event handler checks the current calculation state by reading the CustomSummaryEventArgs.SummaryProcess value. Cell values are added to the total sum only if the corresponding cell under the “Mark” column is checked.

csharp
double sum = 0;
gridView.CustomSummaryCalculate += (sender, e) => {
    GridView view = sender as GridView;
    if (e.IsTotalSummary && (e.Item as GridSummaryItem).FieldName=="Length") {
        GridSummaryItem item = e.Item as GridSummaryItem;
        if (item.FieldName == "Length") {
            switch (e.SummaryProcess) {
                case CustomSummaryProcess.Start:
                    sum = 0;
                    break;
                case CustomSummaryProcess.Calculate:
                    bool shouldSum = (bool)view.GetRowCellValue(e.RowHandle, "Mark");
                    if (shouldSum) {
                        sum += (double)e.FieldValue;
                    }
                    break;
                case CustomSummaryProcess.Finalize:
                    e.TotalValue = sum;
                break;
            }
        }
    }
};
vb
Dim sum As Double = 0
AddHandler gridView.CustomSummaryCalculate, Sub(sender, e)
    Dim view As GridView = TryCast(sender, GridView)
    If e.IsTotalSummary AndAlso (TryCast(e.Item, GridSummaryItem)).FieldName="Length" Then
        Dim item As GridSummaryItem = TryCast(e.Item, GridSummaryItem)
        If item.FieldName = "Length" Then
            Select Case e.SummaryProcess
                Case CustomSummaryProcess.Start
                    sum = 0
                Case CustomSummaryProcess.Calculate
                    Dim shouldSum As Boolean = CBool(view.GetRowCellValue(e.RowHandle, "Mark"))
                    If shouldSum Then
                        sum += CDbl(e.FieldValue)
                    End If
                Case CustomSummaryProcess.Finalize
                    e.TotalValue = sum
            End Select
        End If
    End If
End Sub

To automatically update the summary item when users check or clear the “Mark” column checkboxes, the GridView.UpdateTotalSummary method is called on the RepositoryItem.EditValueChanged event.

csharp
RepositoryItemCheckEdit edit = gridView.Columns["Mark"].RealColumnEdit as RepositoryItemCheckEdit;
edit.EditValueChanged += (sender, e) => {
    //Post an editor's value to a data source
    gridView.PostEditor();
    //Force calculation of the total summary
    gridView.UpdateTotalSummary();
};
vb
Dim edit As RepositoryItemCheckEdit = TryCast(gridView.Columns("Mark").RealColumnEdit, RepositoryItemCheckEdit)
AddHandler edit.EditValueChanged, Sub(sender, e)
    'Post an editor's value to a data source
    gridView.PostEditor()
    'Force calculation of the total summary
    gridView.UpdateTotalSummary()
End Sub

The code below is a complete example code.

csharp
// Handle this event to calculate summary values manually
    double sum = 0;
    gridView.CustomSummaryCalculate += (sender, e) => {
        GridView view = sender as GridView;
        if (e.IsTotalSummary && (e.Item as GridSummaryItem).FieldName=="Length") {
            GridSummaryItem item = e.Item as GridSummaryItem;
            if (item.FieldName == "Length") {
                switch (e.SummaryProcess) {
                    case CustomSummaryProcess.Start:
                        sum = 0;
                        break;
                    case CustomSummaryProcess.Calculate:
                        bool shouldSum = (bool)view.GetRowCellValue(e.RowHandle, "Mark");
                        if (shouldSum) {
                            sum += (double)e.FieldValue;
                        }
                        break;
                    case CustomSummaryProcess.Finalize:
                        e.TotalValue = sum;
                        break;
                }
            }
        }
    };

    gridView.OptionsView.ShowFooter = true;
    GridColumn column = gridView.Columns["Length"];
    column.SummaryItem.SummaryType = SummaryItemType.Custom;

    RepositoryItemCheckEdit edit = gridView.Columns["Mark"].RealColumnEdit as RepositoryItemCheckEdit;
    edit.EditValueChanged += (sender, e) => {
        //Post an editor's value to a data source
        gridView.PostEditor();
        //Force calculation of the total summary
        gridView.UpdateTotalSummary();
    };
vb
' Handle this event to calculate summary values manually
    Dim sum As Double = 0
    AddHandler gridView.CustomSummaryCalculate, Sub(sender, e)
        Dim view As GridView = TryCast(sender, GridView)
        If e.IsTotalSummary AndAlso (TryCast(e.Item, GridSummaryItem)).FieldName="Length" Then
            Dim item As GridSummaryItem = TryCast(e.Item, GridSummaryItem)
            If item.FieldName = "Length" Then
                Select Case e.SummaryProcess
                    Case CustomSummaryProcess.Start
                        sum = 0
                    Case CustomSummaryProcess.Calculate
                        Dim shouldSum As Boolean = CBool(view.GetRowCellValue(e.RowHandle, "Mark"))
                        If shouldSum Then
                            sum += CDbl(e.FieldValue)
                        End If
                    Case CustomSummaryProcess.Finalize
                        e.TotalValue = sum
                End Select
            End If
        End If
    End Sub

    gridView.OptionsView.ShowFooter = True
    Dim column As GridColumn = gridView.Columns("Length")
    column.SummaryItem.SummaryType = SummaryItemType.Custom

    Dim edit As RepositoryItemCheckEdit = TryCast(gridView.Columns("Mark").RealColumnEdit, RepositoryItemCheckEdit)
    AddHandler edit.EditValueChanged, Sub(sender, e)
        'Post an editor's value to a data source
        gridView.PostEditor()
        'Force calculation of the total summary
        gridView.UpdateTotalSummary()
    End Sub