# Grid Aggregates

The Grid component provides built-in aggregates for column values based on [grouping](https://www.telerik.com/blazor-ui/documentation/components/grid/grouping/overview) and also a grand total row.

#### In this article:

* [Available Aggregate Functions](https://www.telerik.com/blazor-ui/documentation/components/grid/grouping/aggregates#available-aggregate-functions)
* [Where You Can Use Aggregates](https://www.telerik.com/blazor-ui/documentation/components/grid/grouping/aggregates#where-you-can-use-aggregates)
* [How to Enable Aggregates](https://www.telerik.com/blazor-ui/documentation/components/grid/grouping/aggregates#how-to-enable-aggregates)
* [Example](https://www.telerik.com/blazor-ui/documentation/components/grid/grouping/aggregates#example)
* [Notes](https://www.telerik.com/blazor-ui/documentation/components/grid/grouping/aggregates#notes)

## Available Aggregate Functions

There are several available aggregate functions under the `Telerik.Blazor.GridAggregateType` enum:

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

You can use aggregates in the following templates:

* [`GroupFooterTemplate`](https://www.telerik.com/blazor-ui/documentation/components/grid/templates/column-group-footer) of a `GridColumn` - a footer in the respective column that renders when the grid is grouped.
* [`GroupHeaderTemplate`](https://www.telerik.com/blazor-ui/documentation/components/grid/templates/group-header) 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.
* [`FooterTemplate`](https://www.telerik.com/blazor-ui/documentation/components/grid/templates/column-footer) of a `GridColumn` - a grand total row of footers for the entire grid.

## Access The Aggregate Values

You can access the aggregate values through the template `context`:

* All templates expose the aggregate values for the current column.
* The `context` of the `GroupHeaderTemplate` and the `GroupFooterTemplate` has an `AggregateResults` property of a type `Dictionary<string, GridGroupAggregateResult>`. This dictionary allows you to access the aggregates for the other columns in the Grid.

## How to Enable Aggregates

To enable aggregates:

1. Under the `GridAggregates` tag, define the `GridAggregate` entries to enable the aggregations per field you want to use.
2. If the Grid is bound to a [dynamic object (Expando)](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/walkthrough-creating-and-using-dynamic-objects), set the `FieldType` attribute of the `GridAggregate` tag (it is of type `Type`).
3. Set the grid's `Groupable` property to `true`.
   * If you will be using only `FooterTemplate`s - grouping is not required.
4. Group the grid to see the effect on group-specific templates.

## Example

> caption Use Aggregates in the Telerik Blazor Grid

<demo metaUrl="client/grid/grouping-aggregates/" height="600"></demo>

## 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, you will get a `null` value.

* If you try to use an aggregate that is not compatible with `Field` type, a runtime error will occur.

* If you update a field of a model the `Data` collection in the view-model, aggregates will not be updated automatically - the grid needs to re-evaluate that data first, and since this is an expensive operation a UI render does not trigger it. You can [update the data collection](https://www.telerik.com/blazor-ui/documentation/components/grid/refresh-data) yourself, or fetching it anew from the service (example [here](https://www.telerik.com/blazor-ui/documentation/components/grid/editing/overview), see how the Create/Update/Delete events fetch data anew).

* If you [bind the Grid via `OnRead` event](https://www.telerik.com/blazor-ui/documentation/components/grid/manual-operations), make sure to set `AggregateResults` in the `GridReadEventArgs` event argument object. Otherwise the Grid will calculate aggregates from the data on the current page only.

```CS.skip-repl
private async Task OnGridRead(GridReadEventArgs args)
{
    DataSourceResult result = await AllGridData.ToDataSourceResultAsync(args.Request);

    args.Data = result.Data;
    args.Total = result.Total;
    args.AggregateResults = result.AggregateResults;
}
```

## See Also

* [Live Demo: Grid Grouping](https://demos.telerik.com/blazor-ui/grid/grouping)
* [Blazor Grid](https://www.telerik.com/blazor-ui/documentation/components/grid/overview)
