How to create weighted summaries in the WinForms GridGroupingControl?
Custom summary
In WinForms GridGroupingControl, 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.
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;
}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