Category / Section
How to create weighted summaries in the WinForms GridGroupingControl?
1 min read
Custom summary
In WinForms GridControl, you can create a custom summary that does weighted average calculations by using a code-naming convention to pass the column holding the weights to the summary descriptor.
C#
void TableDescriptor_QueryCustomSummary(object sender, Syncfusion.Windows.Forms.Grid.Grouping.GridQueryCustomSummaryEventArgs e) { //Check whether the summary type is custom or not. if (e.SummaryDescriptor.SummaryType == SummaryType.Custom) { //Method to calculate weighted summaries. e.SummaryDescriptor.CreateSummaryMethod = new CreateSummaryDelegate(Weightedsummaries.CreateSummaryMethod); } e.Handled = true; } private GridSummaryColumnDescriptor GetWeightedSummaryColumnDescriptor(string sourceCol, string weightCol) { GridSummaryColumnDescriptor wgtSumCol = new GridSummaryColumnDescriptor(); wgtSumCol.Name = string.Format("{0}_{1}", sourceCol, weightCol); //Special name following the convention above. wgtSumCol.DataMember = sourceCol; //The column this summary is applied to. wgtSumCol.DisplayColumn = sourceCol; //Where this summary is displayed. wgtSumCol.Format = "{WeightedAverage:#.##}"; //What is displayed in the summary. wgtSumCol.SummaryType = SummaryType.Custom; //Marks this as a CustomSummary. wgtSumCol.Appearance.AnySummaryCell.HorizontalAlignment = GridHorizontalAlignment.Right; wgtSumCol.MaxLength = 6; return wgtSumCol; }
VB
Private Sub TableDescriptor_QueryCustomSummary(ByVal sender As Object, ByVal e As Syncfusion.Windows.Forms.Grid.Grouping.GridQueryCustomSummaryEventArgs) 'Check whether the summary type is custom or not. If e.SummaryDescriptor.SummaryType = SummaryType.Custom Then 'Method to calculate weighted summaries. e.SummaryDescriptor.CreateSummaryMethod = New CreateSummaryDelegate(Weightedsummaries.CreateSummaryMethod) End If e.Handled = True End Sub Private Function GetWeightedSummaryColumnDescriptor(ByVal sourceCol As String, ByVal weightCol As String) As GridSummaryColumnDescriptor Dim wgtSumCol As New GridSummaryColumnDescriptor() wgtSumCol.Name = String.Format("{0}_{1}", sourceCol, weightCol) 'Special name following the convention above. wgtSumCol.DataMember = sourceCol 'The column this summary is applied to. wgtSumCol.DisplayColumn = sourceCol 'Where this summary is displayed. wgtSumCol.Format = "{WeightedAverage:#.##}" 'What is displayed in the summary. wgtSumCol.SummaryType = SummaryType.Custom 'Marks this as a CustomSummary. wgtSumCol.Appearance.AnySummaryCell.HorizontalAlignment = GridHorizontalAlignment.Right wgtSumCol.MaxLength = 6 Return wgtSumCol End Function
Samples:
Reference link: https://help.syncfusion.com/windowsforms/gridgrouping/summaries#custom-summaries