|
1 | | -# How to show the total hours and minutes of DateTime column in summary row using custom aggregate? |
| 1 | +# How to show the total summary value in DateTime format using custom aggregate in WinForms DataGrid (SfDataGrid) |
2 | 2 |
|
3 | | -## About the example |
| 3 | +## Show the total summary value in DateTime format |
| 4 | +You can implement your own summary aggregate functions using the [GridSummaryColumn.CustomAggregate](https://help.syncfusion.com/cr/windowsforms/Syncfusion.WinForms.DataGrid.GridSummaryColumn.html#Syncfusion_WinForms_DataGrid_GridSummaryColumn_CustomAggregate) property when built-in aggregate functions do not meet your requirement. The summary value can be calculated based on any custom logic. |
4 | 5 |
|
5 | | -This example illustrates how to calculate the total time from a column and display the result in HH:MM format on the table summary row by creating custom summary aggregate. |
| 6 | +In the following example, the Time column of [WinForms DataGrid](https://www.syncfusion.com/winforms-ui-controls/datagrid) (SfDataGrid) displays time in minutes. The custom summary aggregate for the summary column is created to display the total time in HH:MM format. |
| 7 | + |
| 8 | +### C# |
| 9 | + |
| 10 | +```C# |
| 11 | + |
| 12 | +public Form2() |
| 13 | +{ |
| 14 | + InitializeComponent(); |
| 15 | + this.sfDataGrid.DataSource = this.CreateTable(); |
| 16 | + this.sfDataGrid.TableSummaryRows.Add(new GridTableSummaryRow() |
| 17 | + { |
| 18 | + Name = "tableSumamryTrue", |
| 19 | + ShowSummaryInRow = false, |
| 20 | + Title = "Total : {TotalTime}", |
| 21 | + SummaryColumns = new System.Collections.ObjectModel.ObservableCollection<Syncfusion.Data.ISummaryColumn>() |
| 22 | + { |
| 23 | + new GridSummaryColumn() |
| 24 | + { |
| 25 | + Name = "TotalTime", |
| 26 | + CustomAggregate=new CustomSummary(), |
| 27 | + SummaryType=SummaryType.Custom, |
| 28 | + Format="Total time : {TotalHours}", |
| 29 | + MappingName="Time in minutes" |
| 30 | + }, |
| 31 | + } |
| 32 | + }); |
| 33 | +} |
| 34 | + |
| 35 | +public class CustomSummary : ISummaryAggregate |
| 36 | +{ |
| 37 | + public CustomSummary() |
| 38 | + { } |
| 39 | + private string totalHours; |
| 40 | + public string TotalHours { get { return totalHours; } set { totalHours = value; } } |
| 41 | + public Action<IEnumerable, string, PropertyDescriptor> CalculateAggregateFunc() |
| 42 | + { |
| 43 | + return (items, property, pd) => |
| 44 | + { |
| 45 | + var enumerableItems = items as IEnumerable<SalesByYear>; |
| 46 | + |
| 47 | + //To check the summary format of the summary row. |
| 48 | + if (pd.Name == "TotalHours") |
| 49 | + { |
| 50 | + int total = 0; |
| 51 | + foreach (var item in items) |
| 52 | + { |
| 53 | + DataRowView dr = item as DataRowView; |
| 54 | + total += int.Parse(dr["Time in minutes"].ToString()); |
| 55 | + } |
| 56 | + |
| 57 | + this.totalHours = total / 60 + " Hours : " + total % 60 + " minutes"; |
| 58 | + } |
| 59 | + }; |
| 60 | + } |
| 61 | +} |
| 62 | +``` |
| 63 | +### VB |
| 64 | + |
| 65 | +```VB |
| 66 | + |
| 67 | +Public Sub New() |
| 68 | + InitializeComponent() |
| 69 | + Me.sfDataGrid.DataSource = Me.CreateTable() |
| 70 | + 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"}})}) |
| 71 | +End Sub |
| 72 | + |
| 73 | +Public Class CustomSummary |
| 74 | + Implements ISummaryAggregate |
| 75 | + Public Sub New() |
| 76 | + End Sub |
| 77 | + Private _totalHours As String |
| 78 | + Public Property TotalHours() As String |
| 79 | + Get |
| 80 | + Return _totalHours |
| 81 | + End Get |
| 82 | + Set(ByVal value As String) |
| 83 | + _totalHours = value |
| 84 | + End Set |
| 85 | + End Property |
| 86 | + Public Function CalculateAggregateFunc() As Action(Of IEnumerable, String, PropertyDescriptor) Implements ISummaryAggregate.CalculateAggregateFunc |
| 87 | + Return Sub(items, [property], pd) |
| 88 | + If pd.Name = "TotalHours" Then |
| 89 | + Dim total As Integer = 0 |
| 90 | + For Each item In items |
| 91 | + Dim dr As DataRowView = TryCast(item, DataRowView) |
| 92 | + total += Integer.Parse(dr("Time in minutes").ToString()) |
| 93 | + Next item |
| 94 | + Me._totalHours = total \ 60 & " Hours : " & total Mod 60 & " minutes" |
| 95 | + End If |
| 96 | + End Sub |
| 97 | + End Function |
| 98 | +End Class |
| 99 | +``` |
| 100 | + |
0 commit comments