Articles in this section
Category / Section

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

2 mins read

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 in gridgrouping control

Samples:

C#: Custom summary

VB: Custom summary

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