New to Telerik UI for ASP.NET Core? Download free 30-day trial

Aggregates

The Telerik UI Grid for ASP.NET Core component provides built-in aggregates for a grand total row and also column values based on grouping.

In this article:

Available Aggregate Functions

The Telerik UI DataSource used by the Grid exposes several built-in aggregate functions:

  • Average
  • Count
  • Max
  • Min
  • Sum

The Count aggregate can be applied to any type of field. The other aggregates can only be applied to numerical fields (e.g., int, decimal, double, etc.).

Where You Can Use Aggregates

Aggregates can be used for the entire grid via:

  • ClientFooterTemplate of a GridColumn - a grand total row of footers for the entire grid.

Or, to each individual group via:

  • ClientGroupFooterTemplate of a GridColumn - a footer in the respective column that renders when the grid is grouped.
  • ClientGroupHeaderColumnTemplate of a GridColumn - a header in the respective column that renders when the grid is grouped by that column. The value field in the context carries the current group value.

How to Enable Aggregates

To enable aggregates:

  1. Inside the Aggregates option of the data source, define the field and aggregate function.
  2. Use the aggregate result in the templates that support it - their context is strongly typed and carries the aggregate values in the respective fields.
  3. Set the grid's Groupable property to true.
    • If you will be using only FooterTemplates - grouping is not required.
  4. Group the grid to see the effect on group-specific templates.

        @(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.ProductViewModel>()
            .Name("grid")
            .Columns(columns =>
            {
                columns.Bound(p => p.ProductName)
                    .ClientGroupFooterTemplate("Product: #=count#");        
                columns.Bound(p => p.UnitPrice).Format("{0:C}")
                    .ClientFooterTemplate("Total price: #=sum#");
                columns.Bound(p => p.UnitsInStock)
                    .ClientGroupHeaderColumnTemplate("Units In Stock: #= value # (Count: #= count#)")
                    .ClientGroupFooterTemplate("<div>Min: #= min #</div><div>Max: #= max #</div>");
            })
            .Groupable()
            .Pageable()      
            .DataSource(dataSource => dataSource
                .Ajax()
                .Aggregates(aggregates =>
                {
                    aggregates.Add(p => p.ProductName).Count();
                    aggregates.Add(p => p.UnitPrice).Sum();
                    aggregates.Add(p => p.UnitsInStock).Min().Max().Count();
                })
                .Read(read => read.Action("AllProducts", "Grid"))
            )
        )
    

Notes

  • You should define only aggregates that you will use to avoid unnecessary calculations that may be noticeable on large data sets.
  • If you try to use an aggregate that is not defined, or an aggregate over an unsupported field type, a JavaScript exception will be thrown.

See Also

In this article