We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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

Platform: WinForms |
Control: GridGroupingControl
Tags: summaries

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

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile