Is it possible to make grid aggregates based on data out of several columns?

Requirement:

For example, imagine a list of order records, where each record belongs to a customer (many-to-one). Each also has an amount in dollars and a boolean which indicates whether the customer has already paid. Now we want to calculate the percentage of the orders that has been paid already, the percentage of the total amount that has been paid, the average number of orders per customer and so on. These calculations require data from multiple columns. The summary values should be visible at all times in the footer, this should not depend on the user selecting a certain group. Can this be done?

Answer:
You can use custom aggregate function to achieve this requirement. You can call a function inside the FooterTemplate and return the values after performing your calculation to display in Grid’s footer aggregate. Please refer the codes below.

<SfGrid @ref="Grid" DataSource="@Products" AllowPaging="true">

<GridAggregates>

<GridAggregate>

<GridAggregateColumns>

<GridAggregateColumn Field=@nameof(Product.UnitsInStock) Type="AggregateType.Sum">

<FooterTemplate>

@{

var aggregate = (context as AggregateTemplateContext);

<div>

<p>Total units:@CustomdAggregateCalc(aggregate)p>

div>

}

FooterTemplate>

GridAggregateColumn>

GridAggregateColumns>

GridAggregate>

GridAggregates>

...

SfGrid>

@code{

SfGrid<Product> Grid { get; set; }

...

public string CustomdAggregateCalc(AggregateTemplateContext val)

{

//fetch the records with only true values for Discontinued column

var data = Products.Where(e => e.Discontinued == true);

//Now calcluate the value and return it to Footer Template to display in Grid

return Queryable.Sum(data.Select(x => x.UnitsInStock).AsQueryable()).ToString("N2");

}

...

}

Here is a sample for your reference,

https://www.syncfusion.com/downloads/support/directtrac/general/ze/DataGrid1047861799


Loader.
Up arrow icon