# Edit Hierarchical Grids in InCell Mode

## Environment

<table>
 <tr>
  <td>Product</td>
  <td>Telerik UI for ASP.NET MVC Grid</td>
 </tr>
 <tr>
  <td>Product version</td>
  <td>2025.1.227</td>
 </tr>
</table>

## Description

How can I edit hierarchical Grids in InCell edit mode?

## Solution

You can achieve this requirement using the following implementation:

1. Create a hierarchical Grid and configure it for InCell editing:

   ```Razor HtmlHelper
   @(Html.Kendo().Grid<EmployeeViewModel>()
       .Name("Employees")
       .Columns(columns =>
       {
           columns.Bound(e => e.FirstName).Width(140);
           columns.Bound(e => e.LastName).Width(140);
           columns.Bound(e => e.Title).Width(200);
           columns.Bound(e => e.Country).Width(200);
           columns.Bound(e => e.City);
           columns.Command(command =>
               {
                   command.Destroy();
               });
       })
       .ToolBar(tools => {
           tools.Create();
           tools.Save();
       })
       .Scrollable()
       .Editable(e => e.Mode(GridEditMode.InCell))
       .Pageable()
       .Sortable()
       .Filterable()
       .Navigatable()
       .DataSource(source => source
           .Ajax()
           .Model(model =>
           {
               model.Id(e => e.EmployeeID);
               model.Field(e => e.EmployeeID).Editable(false);
           })
           .Read(read => read.Action("Read_Employees", "Employees"))
           .Update(update => update.Action("Update_Employee", "Employees"))
           .Create(create => create.Action("Create_Employee", "Employees"))
           .Destroy(destroy => destroy.Action("Destroy_Employee", "Employees")))
       .ClientDetailTemplateId("OrdersTemplate")
   )
   ```

2. Define the detail Grid within the [Kendo UI template](https://docs.telerik.com/kendo-ui/framework/templates/get-started-external):

   ```Razor HtmlHelper
   <script type="text/kendo-tmpl" id="OrdersTemplate">
   @(Html.Kendo().Grid<OrderViewModel>()
       .Name("Orders_#=EmployeeID#")
       .Columns(columns =>
       {
           columns.Bound(o => o.OrderID).Width(101);
           columns.Bound(o => o.ShipCountry).Width(140);
           columns.Bound(o => o.ShipAddress).Width(200);
           columns.Bound(o => o.ShipName).Width(200);
           columns.Bound(o => o.ShippedDate).Format("{0:d}");
           columns.Command(command =>
           {
               command.Edit();
               command.Destroy();
           });
       })
       .ToolBar(tools => tools.Create())
       .Pageable()
       .Sortable()
       .Filterable()
       .DataSource(source => source
           .Ajax()
           .Model(model =>
           {
               model.Id(o => o.OrderID);
               model.Field(o => o.OrderID).Editable(false);
           })
           .Read(read => read.Action("Read_Orders", "Orders", new { id = "#=EmployeeID#" }))
           .Update(update => update.Action("Update_Order", "Orders"))
           .Create(create => create.Action("Create_Order", "Orders", new { id = "#=EmployeeID#" }))
           .Destroy(destroy => destroy.Action("Destroy_Order", "Orders")))
       .ToClientTemplate()
   )
   </script>
   ```

3. Handle the `keydown` event on the main Grid's table and set the currently focused cell in edit mode when `Enter` is pressed:

   ```JS
       $(function () {
           $("#Employees table").on("keydown", function (e) {
               // If current key is "Enter".
               if (e.keyCode == 13) {
                   // Prevent the "keydown" event action.
                   e.stopImmediatePropagation();
                   var grid = $("#Employees").data("kendoGrid");
                   var currentCell = grid.current();

                   if (currentCell.is(".k-edit-cell")) {
                       setTimeout(function () {
                           grid.closeCell();
                           grid.table.focus();
                       });
                   } else {
                       if (!currentCell.hasClass("k-hierarchy-cell")) {
                           grid.editCell(currentCell);
                       }
                   }
               }
           });
       });
   ```

For review the complete example, refer to the [project on how to edit a hierarchical Grid that is set up for InCell editing](https://github.com/telerik/ui-for-aspnet-mvc-examples/tree/master/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Areas/GridHierarchyInCellEditing) in ASP.NET MVC applications.

## 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 Knowledge Base](https://docs.telerik.com/aspnet-mvc/knowledge-base)
