)
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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to create custom summary at run time in WinForms GridGroupingControl?

Platform: WinForms |
Control: GridGroupingControl |
Published Date: April 18, 2016 |
Last Revised Date: February 12, 2020
Tags: summaries

Custom summary

To add custom summary, the QueryCustomSummary event can be handled. SummaryDescriptor can be used to trigger the summary details in the grid.

The `Total` summary calculate for the column. The sample in this article, explains about the DistinctCount and Median of the CategoryID column.

Create column descriptor for summary and add it in the row descriptor.

C#

GridSummaryColumnDescriptor sd0 = new GridSummaryColumnDescriptor();
sd0.DataMember = "CategoryID";
sd0.DisplayColumn = "CategoryID";
sd0.Format = "{Average:#.00}";
sd0.SummaryType = SummaryType.DoubleAggregate;
this.gridGroupingControl1.TableDescriptor.SummaryRows.Add(new GridSummaryRowDescriptor("Row 0", "Average", sd0));
 
GridSummaryColumnDescriptor sd1 = new GridSummaryColumnDescriptor();
sd1.Name = "QuantityTotal";
sd1.DataMember = "CategoryID";
sd1.DisplayColumn = "CategoryID";
sd1.Format = "{Total}";
sd1.SummaryType = SummaryType.Custom;
this.gridGroupingControl1.TableDescriptor.SummaryRows.Add(new GridSummaryRowDescriptor("Row 1", "Total", sd1));
 
GridSummaryColumnDescriptor sd2 = new GridSummaryColumnDescriptor();
sd2.Name = "QuantityDistinctCount";
sd2.DataMember = "CategoryID";
sd2.DisplayColumn = "CategoryID";
sd2.Format = "{Count}";
sd2.SummaryType = SummaryType.Custom;
this.gridGroupingControl1.TableDescriptor.SummaryRows.Add(new GridSummaryRowDescriptor("Row 2", "DistinctCount", sd2));
 
GridSummaryColumnDescriptor sd3 = new GridSummaryColumnDescriptor();
sd3.Name = "QuantityMedian";
sd3.DataMember = "CategoryID";
sd3.DisplayColumn = "CategoryID";
sd3.Format = "{Median}";
sd3.SummaryType = SummaryType.Custom;
this.gridGroupingControl1.TableDescriptor.SummaryRows.Add(new GridSummaryRowDescriptor("Row 3", "Statistic Median", sd3));

VB

Dim sd0 As New GridSummaryColumnDescriptor()
sd0.DataMember = "CategoryID"
sd0.DisplayColumn = "CategoryID"
sd0.Format = "{Average:#.00}"
sd0.SummaryType = SummaryType.DoubleAggregate
Me.gridGroupingControl1.TableDescriptor.SummaryRows.Add(New GridSummaryRowDescriptor("Row 0", "Average", sd0))
 
Dim sd1 As New GridSummaryColumnDescriptor()
sd1.Name = "QuantityTotal"
sd1.DataMember = "CategoryID"
sd1.DisplayColumn = "CategoryID"
sd1.Format = "{Total}"
sd1.SummaryType = SummaryType.Custom
Me.gridGroupingControl1.TableDescriptor.SummaryRows.Add(New GridSummaryRowDescriptor("Row 1", "Total", sd1))
 
Dim sd2 As New GridSummaryColumnDescriptor()
sd2.Name = "QuantityDistinctCount"
sd2.DataMember = "CategoryID"
sd2.DisplayColumn = "CategoryID"
sd2.Format = "{Count}"
sd2.SummaryType = SummaryType.Custom
Me.gridGroupingControl1.TableDescriptor.SummaryRows.Add(New GridSummaryRowDescriptor("Row 2", "DistinctCount", sd2))
 
Dim sd3 As New GridSummaryColumnDescriptor()
sd3.Name = "QuantityMedian"
sd3.DataMember = "CategoryID"
sd3.DisplayColumn = "CategoryID"
sd3.Format = "{Median}"
sd3.SummaryType = SummaryType.Custom
Me.gridGroupingControl1.TableDescriptor.SummaryRows.Add(New GridSummaryRowDescriptor("Row 3", "Statistic Median", sd3))

Using QueryCustomSummary event

To set the custom summary value, the QueryCustomSummary event can be handled. Custom summary classes for Toatl, DistinctCount and Median can be created and added to the SummaryDescriptor.

this.gridGroupingControl1.QueryCustomSummary += new GridQueryCustomSummaryEventHandler(gridGroupingControl1_QueryCustomSummary);
private void gridGroupingControl1_QueryCustomSummary(object sender, GridQueryCustomSummaryEventArgs e)
{
    switch (e.SummaryColumn.Name)
    {
       case "QuantityTotal":
       {
               e.SummaryDescriptor.CreateSummaryMethod = new CreateSummaryDelegate(TotalSummary.CreateSummaryMethod);
               break;
       }
       case "QuantityDistinctCount":
       {
               e.SummaryDescriptor.CreateSummaryMethod = new CreateSummaryDelegate(DistinctInt32CountSummary.CreateSummaryMethod);
               break;
       }
       case "QuantityMedian":
       {
               e.SummaryDescriptor.CreateSummaryMethod = new CreateSummaryDelegate(StatisticsSummary.CreateSummaryMethod);
               break;
       }
    }
}

VB

Private Me.gridGroupingControl1.QueryCustomSummary += New GridQueryCustomSummaryEventHandler(AddressOf gridGroupingControl1_QueryCustomSummary)
Private Sub gridGroupingControl1_QueryCustomSummary(ByVal sender As Object, ByVal e As GridQueryCustomSummaryEventArgs)
   Select Case e.SummaryColumn.Name
       Case "QuantityTotal"
     e.SummaryDescriptor.CreateSummaryMethod = New CreateSummaryDelegate(TotalSummary.CreateSummaryMethod)
     Exit Select
       Case "QuantityDistinctCount"
     e.SummaryDescriptor.CreateSummaryMethod = New CreateSummaryDelegate(DistinctInt32CountSummary.CreateSummaryMethod)
     Exit Select
       Case "QuantityMedian"
     e.SummaryDescriptor.CreateSummaryMethod = New CreateSummaryDelegate(StatisticsSummary.CreateSummaryMethod)
     Exit Select
    End Select
End Sub

Screenshot

Show the custom summary row

Samples:

C#: Custom summary

VB: Custom summary

Reference link: https://help.syncfusion.com/windowsforms/gridgrouping/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
Live Chat Icon