# Adding 'Select-All' Checkbox in Grid Footer

## 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 2023.2.718 version</td>
 </tr>
</table>

## Description

How can I add a checkbox option in the Telerik UI for ASP.NET MVC Grid footer that allows the user to toggle the selection of all rows? This option is useful when the Grid contains many rows, and the user needs to select or unselect all rows when the Grid is scrolled to the bottom.

## Solution

1. Define a `Select` column in the Grid and add a `checkbox` element in the column's header and footer by using the [`ClientHeaderTemplate()`](https://docs.telerik.com/aspnet-mvc/api/kendo.mvc.ui.fluent/gridboundcolumnbuilder#clientheadertemplatesystemstring) / [`ClientFooterTemplate()`](https://docs.telerik.com/aspnet-mvc/api/kendo.mvc.ui.fluent/gridboundcolumnbuilder#clientfootertemplatesystemstring) options. Ensure that each input has a unique `id` attribute.

   ```Razor HtmlHelper
       @(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.ProductViewModel>()
           .Name("grid")
           .Columns(columns =>
           {
               columns.Select().Width(50)
               .ClientHeaderTemplate("<input tabindex='-1' id='header-check-all' class='k-select-checkbox k-checkbox' data-role='checkbox' aria-label='Select all rows' type='checkbox'/>")
               .ClientFooterTemplate("<input tabindex='-1' id='footer-check-all' class='k-select-checkbox k-checkbox' data-role='checkbox' aria-label='Select all rows' type='checkbox'/>");
               ...
           })
           .PersistSelection()
           ...
       )

   ```

2. Handle the [`DataBound`](https://docs.telerik.com/aspnet-mvc/api/kendo.mvc.ui.fluent/grideventbuilder#databoundsystemstring) event of the Grid and subscribe to the `change` event of each checkbox input.

   ```Razor HtmlHelper
       @(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.ProductViewModel>()
           .Name("grid")
           .Events(ev => ev.DataBound("onDataBound"))
           ...
       )

   ```

   ```JS script
       function onDataBound(e) {
           let gridInstance = e.sender;
           $(gridInstance.element).find("input#footer-check-all").on("change", function (e) { // Footer checkbox "change" event handler.
               ...
           });

           $(gridInstance.element).find("input#header-check-all").on("change", function (e) { // Header checkbox "change" event handler.
               ...
           });
       }
   ```

3. In the `change` event handler of the footer checkbox, use the client-side [`select()`](https://docs.telerik.com/kendo-ui/api/ui/grid/methods/select) and [`clearSelection()`](https://docs.telerik.com/kendo-ui/api/ui/grid/methods/clearselection) methods to select and unselect the Grid rows.

   ```JS script
       function onDataBound(e) {
           let gridInstance = e.sender;
           $(gridInstance.element).find("input#footer-check-all").on("change", function (e) {
               if($(this).is(':checked')) { // If the checkbox is checked
                   let masterRows = $("#grid .k-grid-table .k-master-row"); // Select all Grid rows.
                   gridInstance.select(masterRows);
               } else {
                   gridInstance.clearSelection(); // Clear the selected rows.
               }
           });

           $(gridInstance.element).find("input#header-check-all").on("change", function (e) {
               $(gridInstance.element).find("input#footer-check-all").prop('checked', $(this).is(':checked')); // Toggle the footer checkbox based on the header checkbox.
           });
       }
   ```

For the complete implementation of the suggested approach, refer to the [Telerik REPL example on adding 'Select-All' option in the Grid footer](https://netcorerepl.telerik.com/QHEVwpYA13deEpsX35).

## 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

* [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)
