How to display the calculated result in TableSummaryRow with CustomAggregate in .NET MAUI DataGrid (SfDataGrid) ?
In this article, we will demonstrate how to show calculated result in a TableSummaryRow with CustomAggregate in .NET MAUI DataGrid.
xaml
<ContentPage.BindingContext>
<local:TransactionViewModel x:Name="viewModel"/>
</ContentPage.BindingContext>
<ContentPage.Resources>
<ResourceDictionary>
<local:CustomAggregate x:Key="customAggregate" />
</ResourceDictionary>
</ContentPage.Resources>
<syncfusion:SfDataGrid x:Name="sfGrid" Margin="10"
GridLinesVisibility="Both"
DefaultColumnWidth="120"
HeaderGridLinesVisibility="Both"
AutoGenerateColumnsMode="None"
ItemsSource="{Binding Transactions}">
<syncfusion:SfDataGrid.Columns>
<syncfusion:DataGridNumericColumn MappingName="TransactionId"
Format="#"
HeaderText="ID" />
<syncfusion:DataGridNumericColumn MappingName="Debit"
HeaderText="Debit" />
<syncfusion:DataGridNumericColumn MappingName="Credit"
HeaderText="Credit" />
<syncfusion:DataGridNumericColumn MappingName="Balance"
HeaderText="Balance" />
</syncfusion:SfDataGrid.Columns>
<syncfusion:SfDataGrid.TableSummaryRows>
<syncfusion:DataGridTableSummaryRow
ShowSummaryInRow="False">
<syncfusion:DataGridTableSummaryRow.SummaryColumns>
<syncfusion:DataGridSummaryColumn Name="Debit"
Format="{}{Sum:C0}"
MappingName="Debit"
SummaryType="DoubleAggregate" />
<syncfusion:DataGridSummaryColumn Name="Credit"
Format="{}{Sum:C0}"
MappingName="Credit"
SummaryType="DoubleAggregate" />
</syncfusion:DataGridTableSummaryRow.SummaryColumns>
</syncfusion:DataGridTableSummaryRow>
<syncfusion:DataGridTableSummaryRow ShowSummaryInRow="True"
Title="Balance: {Debit} Db">
<syncfusion:DataGridTableSummaryRow.SummaryColumns>
<syncfusion:DataGridSummaryColumn Name="Debit"
CustomAggregate="{StaticResource customAggregate}"
Format="{}{Balance}"
MappingName="Debit"
SummaryType="Custom" />
</syncfusion:DataGridTableSummaryRow.SummaryColumns>
</syncfusion:DataGridTableSummaryRow>
</syncfusion:SfDataGrid.TableSummaryRows>
</syncfusion:SfDataGrid>
C#
The following code illustrates how to show calculated result in TableSummaryRow with CustomAggregate in DataGrid.
public class CustomAggregate : ISummaryAggregate
{
public double Balance { get; set; }
public Action<System.Collections.IEnumerable, string, System.ComponentModel.PropertyDescriptor> CalculateAggregateFunc()
{
return (items, property, pd) =>
{
var enumerableItems = items as IEnumerable<Transaction>;
if (pd.Name == "Balance")
{
this.Balance = enumerableItems.DebitedTotal<Transaction>(q => (double?)q.Debit) - enumerableItems.CreditedTotal<Transaction>(q => (double?)q.Credit);
}
};
}
}
public static class LinqExtensions
{
public static double DebitedTotal<T>(this IEnumerable<T> values, Func<T, double?> debitedAmount)
{
double? value = 0;
var count = values.Count();
if (count > 0)
{
value = values.Sum(debitedAmount);
}
return (double)value;
}
public static double CreditedTotal<T>(this IEnumerable<T> values, Func<T, double?> creditedAmount)
{
double? value = 0;
var count = values.Count();
if (count > 0)
{
value = values.Sum(creditedAmount);
}
return (double)value;
}
}
Output
Download the complete sample from GitHub
Conclusion
I hope you enjoyed learning how to show calculated result in table summary row with custom aggregate in .NET MAUI DataGrid (SfDataGrid).
You can refer to our .NET MAUI DataGrid feature tour page to learn about its other groundbreaking feature representations. Explore our .NET MAUI DataGrid Documentation to understand how to present and manipulate data.
For current customers, check out our .NET MAUI components on the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to explore our .NET MAUI DataGrid and other .NET MAUI components.
Please let us know in the comments section if you have any queries or require clarification. You can also contact us through our support forums, Direct-Trac or feedback portal, or the feedback portal. We are always happy to assist you!