How to create custom summary at runtime in WinForms GridGroupingControl?
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 the 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.
C#
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;
}
}
}AddHandler Me.gridGroupingControl1.QueryCustomSummary, 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(AddressOf TotalSummary.CreateSummaryMethod)
Case "QuantityDistinctCount"
e.SummaryDescriptor.CreateSummaryMethod = New CreateSummaryDelegate(AddressOf DistinctInt32CountSummary.CreateSummaryMethod)
Case "QuantityMedian"
e.SummaryDescriptor.CreateSummaryMethod = New CreateSummaryDelegate(AddressOf StatisticsSummary.CreateSummaryMethod)
End Select
End SubThe screenshot below illustrates the custom summary creation during runtime.

Samples:
C#: Custom summary
VB: Custom summary
Reference Link: Summaries