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. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to show the total summary value in DateTime format using custom aggregate in WinForms DataGrid (SfDataGrid)?

Platform: WinForms |
Control: SfDataGrid |
Published Date: October 24, 2018 |
Last Revised Date: January 22, 2020

Show the total summary value in DateTime format

You can implement your own summary aggregate functions using the GridSummaryColumn.CustomAggregate property when built-in aggregate functions do not meet your requirement. The summary value can be calculated based on any custom logic.

In the following example, the Time column of data grid displays time in minutes. The custom summary aggregate for the summary column is created to display the total time in HH:MM format.

C#

public Form2()
{
    InitializeComponent();
    this.sfDataGrid.DataSource = this.CreateTable();
    this.sfDataGrid.TableSummaryRows.Add(new GridTableSummaryRow()
    {
        Name = "tableSumamryTrue",
        ShowSummaryInRow = false,
        Title = "Total :  {TotalTime}",
        SummaryColumns = new System.Collections.ObjectModel.ObservableCollection<Syncfusion.Data.ISummaryColumn>()
        {
            new GridSummaryColumn()
            {
                Name = "TotalTime",
               CustomAggregate=new CustomSummary(),
                SummaryType=SummaryType.Custom,
                Format="Total time : {TotalHours}",
                MappingName="Time in minutes"
            },
        }
    });
}
 
public class CustomSummary : ISummaryAggregate
{
    public CustomSummary()
    { }
    private string totalHours;
    public string TotalHours { get { return totalHours; } set { totalHours = value; } }
    public Action<IEnumerable, string, PropertyDescriptor> CalculateAggregateFunc()
    {
        return (items, property, pd) =>
        {
            var enumerableItems = items as IEnumerable<SalesByYear>;
 
            //To check the summary format of the summary row.
            if (pd.Name == "TotalHours")
            {
                int total = 0;
                foreach (var item in items)
                {
                    DataRowView dr = item as DataRowView;
                    total += int.Parse(dr["Time in minutes"].ToString());
                }
 
                this.totalHours = total / 60 + " Hours : " + total % 60 + " minutes";
            }
        };
    }
}

VB

Public Sub New()
 InitializeComponent()
 Me.sfDataGrid.DataSource = Me.CreateTable()
 Me.sfDataGrid.TableSummaryRows.Add(New GridTableSummaryRow() With {.Name = "tableSumamryTrue", .ShowSummaryInRow = False, .Title = "Total :  {TotalTime}", .SummaryColumns =      New System.Collections.ObjectModel.ObservableCollection(Of Syncfusion.Data.ISummaryColumn) (New Syncfusion.Data.ISummaryColumn() {New GridSummaryColumn() With {.Name = "TotalTime",     .CustomAggregate = New CustomSummary(), .SummaryType=SummaryType.Custom, .Format="Total time : {TotalHours}", .MappingName="Time in minutes"}})})
End Sub
 
Public Class CustomSummary
 Implements ISummaryAggregate
 Public Sub New()
 End Sub
    Private _totalHours As String
    Public Property TotalHours() As String
        Get
            Return _totalHours
        End Get
        Set(ByVal value As String)
            _totalHours = value
        End Set
    End Property
    Public Function CalculateAggregateFunc() As Action(Of IEnumerable, String, PropertyDescriptor) Implements ISummaryAggregate.CalculateAggregateFunc
        Return Sub(items, [property], pd)
                   If pd.Name = "TotalHours" Then
                       Dim total As Integer = 0
                       For Each item In items
                           Dim dr As DataRowView = TryCast(item, DataRowView)
                           total += Integer.Parse(dr("Time in minutes").ToString())
                       Next item
                       Me._totalHours = total \ 60 & " Hours : " & total Mod 60 & " minutes"
                   End If
               End Sub
    End Function
End Class

 

Show the total summary value in DateTime format

Sample: How to show the total hours and minutes of DateTime column in summary row using custom aggregate

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