# Show or Hide Grid Aggregates

## Environment

<table>
 <tr>
  <td>Product</td>
  <td>Telerik UI for ASP.NET MVC Grid</td>
 </tr>
 <tr>
  <td>Progress Telerik UI for ASP.NET MVC version</td>
  <td>Created with the 2022.2.621 version</td>
 </tr>
</table>

## Description

How to show and hide the aggregates in the Telerik UI for ASP.NET MVC Grid?

## Solution

1. Hook up for the event that, when triggered, will toggle the aggregates. The example below uses the [`Change`](https://docs.telerik.com/kendo-ui/api/ui/switch/events/change) event of the [Switch](https://demos.telerik.com/aspnet-core/switch) component.
2. Show or hide the aggregates based on a condition. To set up the aggregates, you can use the [`aggregate`](https://docs.telerik.com/kendo-ui/api/data/datasource/methods/aggregate) method of the [DataSource](https://docs.telerik.com/kendo-ui/api/data/datasource). To show or hide the aggregates, you can change the value of the template. The example below uses the [`footerTemplate`](https://docs.telerik.com/kendo-ui/api/ui/grid/configuration/columns.footertemplate) of the Grid in which the aggregate values are displayed.

```Razor Index.cshtml
    @(Html.Kendo().Switch()
        .Name("switch")
        .Events(e => e.Change("onChange"))
        .Messages(c => c.Checked("Show Aggregates").Unchecked("Hide Aggregates"))
        .HtmlAttributes(new {style = "width:150px"})
    )

    @(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.ProductViewModel>()
        .Name("grid")
        .Columns(columns =>
        {
            columns.Bound(p => p.ProductName);
            columns.Bound(p => p.UnitsInStock).Width(100);
            columns.Bound(p => p.Discontinued).Width(100);
            columns.Bound(p => p.UnitPrice).Width(100);
        })
        .Pageable()
        .Scrollable()
        .HtmlAttributes(new { style = "height:430px;" })
        .DataSource(dataSource => dataSource
            .Ajax()
            .PageSize(20)
            .Read(read => read.Action("EditingInline_Read", "Grid"))
        )
    )
```

```JS script.js
    function onChange(e){
        var grid = $("#grid").data("kendoGrid");

        if(e.checked){
            var aggregates = [{ field: "UnitPrice", aggregate: "sum"}];
            grid.dataSource.aggregate(aggregates);
            grid.columns[3].footerTemplate = "Sum: #=data.UnitPrice.sum == 0?'Calculating':data.UnitPrice.sum#";
            grid.setOptions({
                columns: grid.columns
            });
        }
        else{
            grid.columns[3].footerTemplate = "";
            grid.setOptions({
                columns: grid.columns
            });
            grid.dataSource.aggregate([]);
        }
    }
```

For the complete implementation of the suggested approach, refer to the following [Telerik REPL](https://netcorerepl.telerik.com/cGECaibw09Q1ou3W10) example.

## More ASP.NET MVC Grid Resources

* [ASP.NET MVC Grid Documentation](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/data-management/grid/overview)

* [ASP.NET MVC Grid Demos](https://demos.telerik.com/aspnet-mvc/grid/index)

* [ASP.NET MVC Grid Product Page](https://www.telerik.com/aspnet-mvc/grid)

* [Telerik UI for ASP.NET MVC Video Onboarding Course (Free for trial users and license holders)](https://www.telerik.com/aspnet-mvc/documentation/getting-started/video-onboarding)

* [Telerik UI for ASP.NET MVC Forums](https://www.telerik.com/forums/aspnet-mvc)

## See Also

* [Telerik REPL: Show or Hide Grid Aggregates](https://netcorerepl.telerik.com/cGECaibw09Q1ou3W10)
* [Client-Side API Reference of the Grid for ASP.NET MVC](https://docs.telerik.com/kendo-ui/api/ui/grid)
* [Server-Side API Reference of the Grid for ASP.NET MVC](https://docs.telerik.com/aspnet-mvc/api/grid)
* [Telerik UI for ASP.NET MVC Breaking Changes](https://www.telerik.com/aspnet-mvc/documentation/backwards-compatibility/2023-backwards-compatibility)
* [Telerik UI for ASP.NET MVC Knowledge Base](https://docs.telerik.com/aspnet-mvc/knowledge-base)
