How to calculate the summary value based on other column summary value in WinForms DataGrid (SfDataGrid)?
Summaries
By default, the summary value is calculated based on the corresponding summary column in the SfDataGrid. The value of a summary column can be calculated based on other summary column values by changing the display text in the SfDataGrid.DrawCell event with required value. The original summary value of a summary column can be retrieved by using the GetSummaryDisplayText method in SummaryCreator.
The following code example shows how to change the summary column value when ShowInSummaryRow is false.
C#
private void SfDataGrid_DrawCell(object sender, DrawCellEventArgs e) { if ((e.DataRow as DataRowBase).RowType == RowType.TableSummaryRow) { if (e.Column.MappingName == "Q3") { float summaryColumnValue1 = 0; float summaryColumnValue2 = 0; float.TryParse(SummaryCreator.GetSummaryDisplayText((e.DataRow.RowData as SummaryRecordEntry), "Q1", sfDataGrid.View), out summaryColumnValue1); float.TryParse(SummaryCreator.GetSummaryDisplayText((e.DataRow.RowData as SummaryRecordEntry), "Q2", sfDataGrid.View), out summaryColumnValue2); e.DisplayText = (summaryColumnValue1 / summaryColumnValue2).ToString(); } } }
VB
Private Sub SfDataGrid_DrawCell(ByVal sender As Object, ByVal e As DrawCellEventArgs) If (TryCast(e.DataRow, DataRowBase)).RowType = RowType.TableSummaryRow Then If e.Column.MappingName = "Q3" Then Dim summaryColumnValue1 As Single = 0 Dim summaryColumnValue2 As Single = 0 Single.TryParse(SummaryCreator.GetSummaryDisplayText((TryCast(e.DataRow.RowData, SummaryRecordEntry)), "Q1", sfDataGrid.View), summaryColumnValue1) Single.TryParse(SummaryCreator.GetSummaryDisplayText((TryCast(e.DataRow.RowData, SummaryRecordEntry)), "Q2", sfDataGrid.View), summaryColumnValue2) e.DisplayText = (summaryColumnValue1 / summaryColumnValue2).ToString() End If End If End Sub
Samples:
C#: Summary_CS
VB: Summary_VB
The following code example shows how to change the summary column value when ShowInSummaryRow is true.
C#
private void SfDataGrid_DrawCell(object sender, DrawCellEventArgs e) { if ((e.DataRow as DataRowBase).RowType == RowType.TableSummaryCoveredRow) { float summaryColumnValue1 = 0; float summaryColumnValue2 = 0; float.TryParse(SummaryCreator.GetSummaryDisplayText((e.DataRow.RowData as SummaryRecordEntry), "Q1", sfDataGrid.View), out summaryColumnValue1); float.TryParse(SummaryCreator.GetSummaryDisplayText((e.DataRow.RowData as SummaryRecordEntry), "Q2", sfDataGrid.View), out summaryColumnValue2); e.DisplayText = e.DisplayText + " : Ratio " + (summaryColumnValue1 / summaryColumnValue2).ToString(); } }
VB
Private Sub SfDataGrid_DrawCell(ByVal sender As Object, ByVal e As DrawCellEventArgs) If (TryCast(e.DataRow, DataRowBase)).RowType = RowType.TableSummaryCoveredRow Then Dim summaryColumnValue1 As Single = 0 Dim summaryColumnValue2 As Single = 0 Single.TryParse(SummaryCreator.GetSummaryDisplayText((TryCast(e.DataRow.RowData, SummaryRecordEntry)), "Q1", sfDataGrid.View), summaryColumnValue1) Single.TryParse(SummaryCreator.GetSummaryDisplayText((TryCast(e.DataRow.RowData, SummaryRecordEntry)), "Q2", sfDataGrid.View), summaryColumnValue2) e.DisplayText = e.DisplayText & " : Ratio " & (summaryColumnValue1 / summaryColumnValue2).ToString() End If End Sub
Samples:
C#: Summary_CS
VB: Summary_VB
Reference link: https://help.syncfusion.com/windowsforms/datagrid/summaries