Articles in this section
Category / Section

How to create the custom summary based on another column values in WinForms GridGroupingControl?

2 mins read

Custom summaries

By default, the summary values will be added for the entire column. In order to add the summary values based on another column, a custom summary class can be created by implementing the SummaryBase. In the custom summary class, the Combine() method can be used/created to get the updated summary values based on another column values. In the provided sample, the summary value of “SummaryData” column will be calculated based on the checked records.

C#

//Triggering event for CustomSummary.
this.gridGroupingControl1.TableDescriptor.QueryCustomSummary += new GridQueryCustomSummaryEventHandler(gridGroupingControl1_QueryCustomSummary);
//"SummaryData" is summary column and "includeSummary" is condition column.
//Include summary for Checked records.
GridSummaryColumnDescriptor sumColumn = GetSumSummaryColumnDescriptor("SummaryData", "IncludeSummary");   
GridSummaryRowDescriptor rowDescriptor = new GridSummaryRowDescriptor("Row0", "Summary of Checked records only", sumColumn);
rowDescriptor.TitleColumnCount = 2;
//Adding summary row to the table descriptor
this.gridGroupingControl1.TableDescriptor.SummaryRows.Add(rowDescriptor);
//Event customization for CustomSummary
private void gridGroupingControl1_QueryCustomSummary(object sender, GridQueryCustomSummaryEventArgs e)
{
    //checking for custom summary.
    if (e.SummaryDescriptor.SummaryType == SummaryType.Custom)  
    {
        //Sets the custom summary method for custom calculations.
        e.SummaryDescriptor.CreateSummaryMethod =
            new CreateSummaryDelegate(CustomSummary.CreateSummaryMethod); 
    }
    e.Handled = true;
}
//CustomSummary class
public sealed class CustomSummary : SummaryBase
{ 
    public CustomSummary()
    {
 
    }
    //Method for custom summary calculation.
    public static ISummary CreateSummaryMethod(SummaryDescriptor sd, Record record) 
    {
        object obj = sd.GetValue(record);
        bool isNull = (obj == null || obj is DBNull);
        if (isNull)
        {
             return Empty;
        }
        else
        {
             int i = sd.Name.LastIndexOf('_') + 1; 
             string conditionColumn = sd.Name.Substring(i);
             object obj1 = record.GetValue(conditionColumn);
              if (obj1 == null)
              {
                   throw new ArgumentException(string.Format("[{0}] not a column.", conditionColumn));
               }
        //DefaultValue of summary if unchecked.
        int sumValue = 0;
        if (record.GetValue(conditionColumn).ToString() == "True")
             //Summary value of record if it is checked.
            sumValue = Convert.ToInt32(obj);   
         return new CustomSummary(sumValue);
        }
    }
}

 

VB

'Triggering event for CustomSummary.
Private Me.gridGroupingControl1.TableDescriptor.QueryCustomSummary += New GridQueryCustomSummaryEventHandler(AddressOf gridGroupingControl1_QueryCustomSummary)
'"SummaryData" is summary column and "includeSummary" is condition column.
'Include summary for Checked records.
Private sumColumn As GridSummaryColumnDescriptor = GetSumSummaryColumnDescriptor("SummaryData", "IncludeSummary")
Private rowDescriptor As New GridSummaryRowDescriptor("Row0", "Summary of Checked records only", sumColumn)
rowDescriptor.TitleColumnCount = 2
'Adding summary row to the table descriptor
Me.gridGroupingControl1.TableDescriptor.SummaryRows.Add(rowDescriptor)
'Event customization for CustomSummary
private void gridGroupingControl1_QueryCustomSummary(Object sender, GridQueryCustomSummaryEventArgs e)
    'checking for custom summary.
    If e.SummaryDescriptor.SummaryType = SummaryType.Custom Then
        'Sets the custom summary method for custom calculations.
        e.SummaryDescriptor.CreateSummaryMethod = New CreateSummaryDelegate(CustomSummary.CreateSummaryMethod)
    End If
    e.Handled = True
'CustomSummary class
Public NotInheritable Class CustomSummary
    Inherits SummaryBase
    Public Sub New()
    End Sub
    'Method for custom summary calculation.
    public Shared Function ISummary CreateSummaryMethod(SummaryDescriptor sd, Record record)
        Dim obj As Object = sd.GetValue(record)
        Dim isNull As Boolean = (obj Is Nothing OrElse TypeOf obj Is DBNull)
        If isNull Then
            Return Empty
        Else
            Dim i As Integer = sd.Name.LastIndexOf("_"c) + 1
            Dim conditionColumn As String = sd.Name.Substring(i)
            Dim obj1 As Object = record.GetValue(conditionColumn)
            If obj1 Is Nothing Then
                Throw New ArgumentException(String.Format("[{0}] not a column.", conditionColumn))
            End If
           'DefaultValue of summary if unchecked.
            Dim sumValue As Integer = 0
            If record.GetValue(conditionColumn).ToString() = "True" Then
                'Summary value of record if it is checked.
                sumValue = Convert.ToInt32(obj)
            End If
            Return New CustomSummary(sumValue)
        End If
    End Function
End Class

 

Screenshot

Custom summaries of GridGroupingControl

Samples:

C#: CustomSummary_CS

VB: CustomSummary_VB

Reference link: https://help.syncfusion.com/windowsforms/classic/gridgroupingcontrol/summaries#custom-summaries

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied