Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 98 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,100 @@
# How to show the total hours and minutes of DateTime column in summary row using custom aggregate?
# How to show the total summary value in DateTime format using custom aggregate in WinForms DataGrid (SfDataGrid)

## About the example
## Show the total summary value in DateTime format
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.

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.
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.

### C#

```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

```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
```