# Updating Grid Cell Dynamically based on the Values of Multiple Cells

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

## Description

How can I dynamically update a specified non-editable cell, whose value is a calculation based on other editable cells, when using Telerik UI for ASP.NET MVC Grid in InCell editable mode?

## Solution

The InCell editable Grid contains the following columns:

* <b>CreditAmount</b>—An editable column.
* <b>DebitAmount</b>—A non-editable column.
* <b>TransactionAmount</b>—A non-editable column which value equals subtraction of the <b>DebitAmount</b> from the <b>CreditAmount</b> value (<b>CreditAmount</b> - <b>DebitAmount</b>).

Follow the next steps to update the <b>TransactionAmount</b> when the <b>CreditAmount</b> changes.

1. Add a custom class to the <b>CreditAmount</b> column and disable the <b>TransactionAmount</b> by using the `k-disabled` class.

   ```Razor HtmlHelper
       @(Html.Kendo().Grid<GridViewModel>()
           .Name("grid")
           .Columns(columns =>
           {
               columns.Bound(p => p.CreditAmount).HtmlAttributes(new { @class="creditAmountCell"});
               columns.Bound(p => p.DebitAmount);
               columns.Bound(p => p.TransactionAmount).HtmlAttributes(new { @class = "k-disabled", style = "opacity: 1"});                         
           })
           .Editable(e => e.Mode(GridEditMode.InCell))
           ...
           .DataSource(dataSource => dataSource
               .Ajax()
               .Model(m => 
               {
                   m.Id("OrderID");
                   m.Field(p => p.OrderID).Editable(false);
                   m.Field(p => p.DebitAmount).Editable(false);
               })
               ...
           )
       )
   ```

2. Handle the `CellClose` event of the Grid that triggers when a specified cell in edit mode is going to be closed.

3. Check if the closed cell contains the custom class of the <b>CreditAmount</b> column and if its value has changed.

4. Use [`set()`](https://docs.telerik.com/kendo-ui/api/data/observableobject/methods/set) method to update the <b>TransactionAmount</b> column.

   ```Razor HtmlHelper
       @(Html.Kendo().Grid<GridViewModel>()
           .Name("grid")
           .Events(ev => ev.CellClose("onCellClose"))
           ...
       )
   ```

   ```JS script
       <script>
           function onCellClose(e) {
               var closedCreditAmountCell = $(e.container).hasClass("creditAmountCell");
               if (closedCreditAmountCell && e.model.dirtyFields["CreditAmount"] == true) { // Check if "CreditAmount" cell is updated.
                   let creditAmountValue = e.model.CreditAmount; // Get the new CreditAmount value.
                   let debitAmountValue = e.model.DebitAmount; // Get the respective DebitAmount value.
                   setTimeout(() => {
                       e.model.set("TransactionAmount", (creditAmountValue - debitAmountValue)); // Update the "TransactionAmount" cell.
                   }, 100);
               }
           }
       </script>
   ```

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