# Grid Grouping with Responsive Columns

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

## Description

When I utilize the approach suggested in the [Grid Responsive Columns Demo](https://demos.telerik.com/aspnet-mvc/grid/responsive-columns) I lose the ability to group the Grid's columns. How can I solve this?

## Solution

The ability to group the Grid is lost when the **responsive-column-template** is in use because the original columns of the Grid are no longer displayed. To workaround this you can define a MultiSelect Component over the Grid and subscribe to its [`Change Еvent`](https://docs.telerik.com/aspnet-core/api/kendo.mvc.ui.fluent/multiselecteventbuilder#changesystemstring) to group the Grid by its selected values.

1. Bind the MultiSelect to a **List** of the Grid columns' names.

   ```Razor HtmlHelper
   @(Html.Kendo().MultiSelect()
           .Name("group")
           .Placeholder("Group by...")
           .BindTo(new List<string>() {
               "ContactName",
               "ContactTitle",
               "CompanyName",
               "Country"
           })
           .Events(e => e.Change("onChange"))
   )
   ```

2. In the **onChange** handler create an object from the selected [`values`](https://docs.telerik.com/kendo-ui/api/ui/multiselect/methods/value) of the MultiSelect and push that into an array. Then pass that array to dataSource's [`group method`](https://docs.telerik.com/kendo-ui/api/data/datasource/methods/group).

   ```JavaScript
       function onChange(e){
           var grid = $("#grid").getKendoGrid(); 
           var groupedBy = [];
           var values = e.sender.value();
           if(values.length>0){
               values.forEach(value=>{
                   groupedBy.push({field:value})
               })
           }
           grid.dataSource.group(groupedBy)
       }
   ```

3. Subscribe to the `resize` event of the Web API's Window to show the MultiSelect when the Grid's columns collapse and also ensure that columns that have been grouped are persisted in the new responsive layout.

   ```JavaScript
       $(window).on("resize",function(e){
           if(parseInt($(".k-body").css("width"),10)<450){
               $(".k-grouping-header").css("display","none");
               $(".group").css("display","block");
               var grid = $("#grid").getKendoGrid(); 
               var ms = $("#group").getKendoMultiSelect();
               var groups = grid.dataSource.group();
               if(groups.length>0){
                   groups.forEach(group=>{
                       ms.value([ms.value(),group.field])
                   })
               }
           }else{
               $(".k-grouping-header").css("display","block");
               $(".group").css("display","none");
           }
       })
   ```

To explore the complete behavior, see the Telerik REPL example on how to [enable Grid Grouping when Responsive Columns are employed](https://netcorerepl.telerik.com/cllvvsvf38K7h8Bt48).

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