)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to calculate the summary value based on other column summary value in WinForms DataGrid (SfDataGrid)?

Platform: WinForms |
Control: SfDataGrid |
Published Date: May 31, 2018 |
Last Revised Date: January 13, 2020
Tags: summary

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

 

Shows the summary column value

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 

 

Shows the summary column value

Samples:

C#: Summary_CS

VB: Summary_VB

Reference link: https://help.syncfusion.com/windowsforms/datagrid/summaries

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon