)
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 show the sum of the two column values in the summary in WinForms GridGroupingControl?

Platform: WinForms |
Control: GridGroupingControl |
Published Date: September 26, 2016 |
Last Revised Date: December 24, 2019
Tags: summaries

Summaries

In order to show the sum of the two columns value in the summary, custom summary can be used to customize the summary values. To create the custom summary, you can use the SummaryType.Custom option for summary row descriptor.

 

The custom summary can be created by deriving the SummaryBase class and override the methods of it. For more information about the custom summary, refer this link.

C#

//Sum of col2 and Col3
GridSummaryColumnDescriptor sumCol2 = GetSumSummaryColumnDescriptor("SampleData1", "SampleData2");
GridSummaryRowDescriptor rowDescriptor = new GridSummaryRowDescriptor("Row0", "Sum of SampleData1 and SampleData2 columns", new GridSummaryColumnDescriptor[] { sumCol2 });
this.gridGroupingControl1.TableDescriptor.SummaryRows.Add(rowDescriptor);
/// <summary>
/// Creating a CustomSummaary method.
/// </summary>
private void gridGroupingControl1_QueryCustomSummary(object sender, GridQueryCustomSummaryEventArgs e)
{
    if (e.SummaryDescriptor.SummaryType == SummaryType.Custom)
    {
        e.SummaryDescriptor.CreateSummaryMethod =
            new CreateSummaryDelegate(CustomSummary.CreateSummaryMethod);
    }
    e.Handled = true;
}
/// <summary>
/// Applying WeightedSummary for Given Columns
/// </summary>
private GridSummaryColumnDescriptor GetSumSummaryColumnDescriptor(string col1, string col2)
{
    GridSummaryColumnDescriptor col2Summary = new GridSummaryColumnDescriptor();
    col2Summary.Name = string.Format("{0}_{1}", col1, col2); //special name following the convention above
    col2Summary.DataMember = col1; //the column this summary is applied to
    col2Summary.DisplayColumn = col1; //where this summary is displayed
    col2Summary.Format = "{TotalSummary:#.##}"; //what is displayed in the summary
    col2Summary.SummaryType = SummaryType.Custom; //marks this as a CustomSummary
    col2Summary.Appearance.AnySummaryCell.HorizontalAlignment = GridHorizontalAlignment.Right;
    col2Summary.MaxLength = 6;
    return col2Summary;
}

 

VB

'Sum of col2 and Col3
Private sumCol2 As GridSummaryColumnDescriptor = GetSumSummaryColumnDescriptor("SampleData1", "SampleData2")
Private rowDescriptor As New GridSummaryRowDescriptor("Row0", "Sum of SampleData1 and SampleData2 columns", New GridSummaryColumnDescriptor() { sumCol2 })
Me.gridGroupingControl1.TableDescriptor.SummaryRows.Add(rowDescriptor)
''' <summary>
''' Creating a CustomSummaary method.
''' </summary>
private void gridGroupingControl1_QueryCustomSummary(Object sender, GridQueryCustomSummaryEventArgs e)
           If e.SummaryDescriptor.SummaryType = SummaryType.Custom Then
             e.SummaryDescriptor.CreateSummaryMethod = New CreateSummaryDelegate(AddressOf CustomSummary.CreateSummaryMethod)
           End If
           e.Handled = True
''' <summary>
''' Applying WeightedSummary for Given Columns
''' </summary>
private GridSummaryColumnDescriptor GetSumSummaryColumnDescriptor(String col1, String col2)
            Dim col2Summary As New GridSummaryColumnDescriptor()
            col2Summary.Name = String.Format("{0}_{1}", col1, col2) 'special name following the convention above
            col2Summary.DataMember = col1 'the column this summary is applied to
            col2Summary.DisplayColumn = col1 'where this summary is displayed
            col2Summary.Format = "{TotalSummary:#.##}" 'what is displayed in the summary
            col2Summary.SummaryType = SummaryType.Custom 'marks this as a CustomSummary
            col2Summary.Appearance.AnySummaryCell.HorizontalAlignment = GridHorizontalAlignment.Right
            col2Summary.MaxLength = 6
            Return col2Summary

 

CustomSummary class customization

C#

/// <summary>
/// Syncfusion Custom Summary class that computes the weighted average of the of entries 
/// where the weights come in from a column passed in through a naming convention. 
/// </summary>
public sealed class CustomSummary : SummaryBase
{
    #region "API Definition"
 
    double _valTotal;
    double _wgtTotal;
    public static readonly CustomSummary Empty = new CustomSummary(0, 0);
 
    #endregion
       
    #region Constructor
    public CustomSummary()
    {
 
    }
    /// <summary>
    /// Initializes a new summary object.
    /// </summary>
    /// <param name="valTotal"></param>
    /// <param name="wgtTotal"></param>
    public CustomSummary(double valTotal, double wgtTotal)
    {
        _wgtTotal = wgtTotal;
        _valTotal = valTotal;
    }
 
    #endregion
        
    #region CreateSummaryMethod
    /// <summary>
    /// Assign this CreateSummaryDelegate handler method to SummaryDescriptor.CreateSummaryMethod 
    /// </summary>
    /// <param name="sd"></param>
    /// <param name="record"></param>
    /// <returns></returns>
    public static ISummary CreateSummaryMethod(SummaryDescriptor sd, Record record)
    {
            object obj = sd.GetValue(record);
            bool isNull = (obj == null || obj is DBNull);
            if (isNull)
            {
                     return Empty;
            }
            else
            {
                     int i = sd.Name.LastIndexOf('_') + 1;
                     string col2 = sd.Name.Substring(i);
                     object obj1 = record.GetValue(col2);
                     if (obj1 == null)
                     {
                              throw new ArgumentException(string.Format("[{0}] not a column.", col2));
                     }
               
                     double col2Value = Convert.ToDouble(obj1);
                     double col1 = Convert.ToDouble(obj);
 
                      return new CustomSummary(col1, col2Value);
             }
    }
 
    #endregion
 
    #region "Weighted Summary Calculation"        
    /// <summary>
    /// The running weighted sum of this summary
    /// </summary>
    public double TotalSummary
    {
           get
 {
                    if (_wgtTotal == 0)
                        return _wgtTotal;
                    return _valTotal + _wgtTotal;
            }
    }
    /// <summary>
    /// Combines this summary information with another objects summary and returns a new object.  
    /// </summary>
    /// <param name="other"></param>
    /// <returns></returns>
    /// <remarks>
    /// This method must honor the immutable characteristics of summary objects and return 
    /// a new summary object instead of modifying an existing summary object.
    /// </remarks>
    public override SummaryBase Combine(SummaryBase other)
    {
            return Combine((CustomSummary)other);
    }
    /// <summary>
    /// Combines this summary information with another objects summary and returns a new object.  
    /// </summary>
    /// <remarks>
    /// This method must honor the immutable characteristics of summary objects and return 
    /// a new summary object instead of modifying an existing summary object.
    /// </remarks>
    public CustomSummary Combine(CustomSummary other)
    {
                  // Summary objects are immutable. That means properties cannot be modified for an 
                  // existing object. Instead every time a change is made a new object must be created (just like 
                  // System.String). 
                  return new CustomSummary(this._valTotal + other._valTotal, this._wgtTotal + other._wgtTotal);
      }
 
      /// <override/>
      public override string ToString()
      {
               return String.Format("TotalSummary = {0:0.00}", TotalSummary);
       }
    #endregion
}

 

VB

''' <summary>
''' Syncfusion Custom Summary class that computes the weighted average of the of entries 
''' where the weights come in from a column passed in through a naming convention. 
''' </summary>
Public NotInheritable Class CustomSummary
      Inherits SummaryBase
      #region "API Definition"
 
      Private _valTotal As Double
      Private _wgtTotal As Double
      Public Shared ReadOnly Empty As New CustomSummary(0, 0)
 
      #End Region
 
      #Region "Constructor"
      Public Sub New()
      End Sub
      ''' <summary>
      ''' Initializes a new summary object.
      ''' </summary>
      ''' <param name="valTotal"></param>
      ''' <param name="wgtTotal"></param>
      Public Sub New(ByVal valTotal As Double, ByVal wgtTotal As Double)
            _wgtTotal = wgtTotal
            _valTotal = valTotal
      End Sub
 
      #End Region
 
      #Region "CreateSummaryMethod"
            ''' <summary>
            ''' Assign this CreateSummaryDelegate handler method to SummaryDescriptor.CreateSummaryMethod 
            ''' </summary>
            ''' <param name="sd"></param>
            ''' <param name="record"></param>
            ''' <returns></returns>
            Public Shared Function CreateSummaryMethod(ByVal sd As SummaryDescriptor, ByVal record As Record) As ISummary
                    Dim obj As Object = sd.GetValue(record)
                    Dim isNull As Boolean = (obj Is Nothing OrElse TypeOf obj Is DBNull)
                    If isNull Then
                               Return Empty
                    Else
                                Dim i As Integer = sd.Name.LastIndexOf("_"c) + 1
                                Dim col2 As String = sd.Name.Substring(i)
                                Dim obj1 As Object = record.GetValue(col2)
                                If obj1 Is Nothing Then
                                           Throw New ArgumentException(String.Format("[{0}] not a column.", col2))
                                End If
 
                                Dim col2Value As Double = Convert.ToDouble(obj1)
                                Dim col1 As Double = Convert.ToDouble(obj)
                                Return New CustomSummary(col1, col2Value)
                    End If
             End Function
 
            #End Region
 
            #region "Weighted Summary Calculation"
            ''' <summary>
            ''' The running weighted sum of this summary
            ''' </summary>
            Public ReadOnly Property TotalSummary() As Double
                    Get
                               If _wgtTotal = 0 Then
                                          Return _wgtTotal
                               End If
                                          Return _valTotal + _wgtTotal
                    End Get
            End Property
            ''' <summary>
            ''' Combines this summary information with another objects summary and returns a new object.  
            ''' </summary>
            ''' <param name="other"></param>
            ''' <returns></returns>
            ''' <remarks>
            ''' This method must honor the immutable characteristics of summary objects and return 
            ''' a new summary object instead of modifying an existing summary object.
            ''' </remarks>
 Public Overrides Function Combine(ByVal other As SummaryBase) As SummaryBase
                     Return Combine(CType(other, CustomSummary))
           End Function
            ''' <summary>
            ''' Combines this summary information with another objects summary and returns a new object.  
            ''' </summary>
            ''' <remarks>
            ''' This method must honor the immutable characteristics of summary objects and return 
            ''' a new summary object instead of modifying an existing summary object.
            ''' </remarks>
            Public Function Combine(ByVal other As CustomSummary) As CustomSummary
                     ' Summary objects are immutable. That means properties cannot be modified for an 
                     ' existing object. Instead every time a change is made a new object must be created (just like 
                     ' System.String).   
                     Return New CustomSummary(Me._valTotal + other._valTotal, Me._wgtTotal + other._wgtTotal)
            End Function
            ''' <override/>
            Public Overrides Function ToString() As String
                    Return String.Format("TotalSummary = {0:0.00}", TotalSummary)
            End Function
           #End Region
End Class

Screenshot

Show the summaries for two columns

Samples:

C#: Summary_of_Two_Columns

VB: Summary_of_Two_Columns

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