|
| 1 | +--- |
| 2 | +title: Row Selection in Edit with InCell EditMode |
| 3 | +description: How to Select a row that is being edited in InCell editing mode |
| 4 | +type: how-to |
| 5 | +page_title: Row Selection in Edit with InCell EditMode |
| 6 | +slug: grid-kb-row-select-incell-edit |
| 7 | +position: |
| 8 | +tags: |
| 9 | +res_type: kb |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | +<table> |
| 14 | + <tbody> |
| 15 | + <tr> |
| 16 | + <td>Product</td> |
| 17 | + <td>Grid for Blazor</td> |
| 18 | + </tr> |
| 19 | + </tbody> |
| 20 | +</table> |
| 21 | + |
| 22 | +## Description |
| 23 | + |
| 24 | +When using the [InCell]({%slug components/grid/editing/incell%}) Editing Mode, I want the row that is currently edited to be selected. I want the user to get the current row selected when they edit it. |
| 25 | + |
| 26 | + By default, the click action opens a cell for editing and does not select a row to avoid an ambiguous action, and so rows can only be selected with the dedicated grid selection column. |
| 27 | + |
| 28 | + |
| 29 | +## Solution |
| 30 | + |
| 31 | +Use the `OnEdit` and `OnUpdate` [Grid events]({%slug grid-events%}#cud-events): |
| 32 | +* In the handler for the `OnEdit` event add the currently edited item, passed to the method through the object of type `GridCommandEventArgs`, into the `SelectedItems` collection. |
| 33 | + * The item added to the collection is with the old value, before the editing. |
| 34 | +* In the handler for the `OnUpdate` event, update the `SelectedItems` collection with the new value of the edited item to ensure data integrity. |
| 35 | + |
| 36 | +>caption How to Select the row that is being edited in InCell edit mode |
| 37 | +
|
| 38 | +````CSHTML |
| 39 | +@* You can create your own extension method to add an item into IEnumerable collection without the usage of a mediator one. *@ |
| 40 | +
|
| 41 | +<TelerikGrid Data="@GridData" |
| 42 | + Height="400px" |
| 43 | + SelectionMode="GridSelectionMode.Multiple" |
| 44 | + @bind-SelectedItems="@SelectedItems" |
| 45 | + EditMode="GridEditMode.Incell" |
| 46 | + OnUpdate="@UpdateItem" |
| 47 | + OnEdit="@EditHandler"> |
| 48 | + <GridColumns> |
| 49 | + <GridCheckboxColumn SelectAllMode="@GridSelectAllMode.All"></GridCheckboxColumn> |
| 50 | + <GridColumn Field=@nameof(Product.ProductId) Title="Product Id" Editable="false" /> |
| 51 | + <GridColumn Field=@nameof(Product.ProductName) Title="Product Name" /> |
| 52 | + <GridColumn Field=@nameof(Product.SupplierId) Title="Supplier Id" /> |
| 53 | + <GridColumn Field=@nameof(Product.UnitPrice) Title="Unit Price" /> |
| 54 | + <GridColumn Field=@nameof(Product.UnitsInStock) Title="Units in stock" /> |
| 55 | + </GridColumns> |
| 56 | +</TelerikGrid> |
| 57 | +
|
| 58 | +@if (SelectedItems.Any()) |
| 59 | +{ |
| 60 | + <ul> |
| 61 | + @foreach (Product item in SelectedItems) |
| 62 | + { |
| 63 | + <li>@item.ProductName</li> |
| 64 | + } |
| 65 | + </ul> |
| 66 | +} |
| 67 | +
|
| 68 | +
|
| 69 | +@code { |
| 70 | + public void EditHandler(GridCommandEventArgs args) |
| 71 | + { |
| 72 | + var item = args.Item as Product; |
| 73 | + var foundItem = SelectedItems.Where(x => x.ProductId == item.ProductId).FirstOrDefault(); |
| 74 | +
|
| 75 | + if (foundItem == null) |
| 76 | + { |
| 77 | + // add the currently edited row to the selected items |
| 78 | + var selItemsList = SelectedItems.ToList(); |
| 79 | + selItemsList.Add(item); |
| 80 | + SelectedItems = new List<Product>(selItemsList); |
| 81 | + } |
| 82 | + } |
| 83 | +
|
| 84 | + public void UpdateItem(GridCommandEventArgs args) |
| 85 | + { |
| 86 | + var item = args.Item as Product; |
| 87 | + int index = GridData.FindIndex(x => item.ProductId == x.ProductId); |
| 88 | + var currentSelectedItems = new List<Product>(SelectedItems); |
| 89 | + int selectedItemIndex = currentSelectedItems.FindIndex(x => x.ProductId == item.ProductId); |
| 90 | +
|
| 91 | + if (index != -1) |
| 92 | + { |
| 93 | + // update the selected items collection |
| 94 | + currentSelectedItems[selectedItemIndex] = item; |
| 95 | + SelectedItems = currentSelectedItems; |
| 96 | +
|
| 97 | + // The actual Update operation for the view-model data. Add your actual data source operations here |
| 98 | + GridData[index] = item; |
| 99 | + } |
| 100 | + } |
| 101 | +
|
| 102 | + public List<Product> GridData { get; set; } |
| 103 | + public IEnumerable<Product> SelectedItems { get; set; } = new List<Product>(); |
| 104 | +
|
| 105 | + protected override void OnInitialized() |
| 106 | + { |
| 107 | + GridData = GenerateProducts(); |
| 108 | + } |
| 109 | +
|
| 110 | + private List<Product> GenerateProducts() |
| 111 | + { |
| 112 | + List<Product> products = new List<Product>(); |
| 113 | +
|
| 114 | + for (int i = 1; i <= 100; i++) |
| 115 | + { |
| 116 | + var product = new Product() |
| 117 | + { |
| 118 | + ProductId = i, |
| 119 | + ProductName = "Product " + i.ToString(), |
| 120 | + SupplierId = i, |
| 121 | + UnitPrice = (decimal)(i * 3.14), |
| 122 | + UnitsInStock = (short)(i * 1), |
| 123 | + }; |
| 124 | + products.Add(product); |
| 125 | + } |
| 126 | + return products; |
| 127 | + } |
| 128 | +
|
| 129 | + public class Product |
| 130 | + { |
| 131 | + public int ProductId { get; set; } |
| 132 | + public string ProductName { get; set; } |
| 133 | + public int SupplierId { get; set; } |
| 134 | + public decimal UnitPrice { get; set; } |
| 135 | + public short UnitsInStock { get; set; } |
| 136 | + } |
| 137 | +} |
| 138 | +```` |
0 commit comments