This example demonstrates how to create a custom editor inside column's DataItem template when GridView is in Batch Edit mode.
You can implement the EditItem template for a column by performing the following steps:
1. Specify column's EditItem template:
[C#]settings.Columns.Add(column=> { column.FieldName="C1"; column.SetEditItemTemplateContent(c=> { @Html.DevExpress().SpinEdit(spinSettings=> { spinSettings.Name="C1spinEdit"; spinSettings.Width=System.Web.UI.WebControls.Unit.Percentage(100); spinSettings.Properties.ClientSideEvents.KeyDown="C1spinEdit_KeyDown"; spinSettings.Properties.ClientSideEvents.LostFocus="C1spinEdit_LostFocus"; }).Render(); }); });
2. Handle grid's client-side BatchEditStartEditing event to set the grid's cell values to the editor. It is possible to get the focused cell value using the e.rowValues property:
[JScript]function Grid_BatchEditStartEditing(s, e){ var productNameColumn = s.GetColumnByField("C1"); if(!e.rowValues.hasOwnProperty(productNameColumn.index)) return; var cellInfo = e.rowValues[productNameColumn.index]; C1spinEdit.SetValue(cellInfo.value); if(e.focusedColumn === productNameColumn) C1spinEdit.SetFocus(); }
3. Handle the BatchEditEndEditing event to pass the value entered in the editor to the grid's cell:
[JScript]function Grid_BatchEditEndEditing(s, e){var productNameColumn = s.GetColumnByField("C1");if(!e.rowValues.hasOwnProperty(productNameColumn.index))return;var cellInfo = e.rowValues[productNameColumn.index]; cellInfo.value = C1spinEdit.GetValue(); cellInfo.text = C1spinEdit.GetText(); C1spinEdit.SetValue(null);}
4. The BatchEditRowValidating event allows validating the grid's cell based on the entered value:
[JScript]function Grid_BatchEditRowValidating(s, e){var productNameColumn = s.GetColumnByField("C1");var cellValidationInfo = e.validationInfo[productNameColumn.index];if(!cellValidationInfo)return;var value = cellValidationInfo.value;if(!ASPxClientUtils.IsExists(value) || ASPxClientUtils.Trim(value) === ""){ cellValidationInfo.isValid = false; cellValidationInfo.errorText = "C1 is required";}}
5. Finally, handle the editor's client-side KeyDown and LostFocus events to emulate the behavior of standard grid editors when an end-user uses a keyboard or mouse:
[JScript]var preventEndEditOnLostFocus = false;function C1spinEdit_KeyDown(s, e){var keyCode = ASPxClientUtils.GetKeyCode(e.htmlEvent);if(keyCode !== 9 && keyCode !== 13)return;var moveActionName = e.htmlEvent.shiftKey ? "MoveFocusBackward" : "MoveFocusForward";if(grid.batchEditApi[moveActionName]()){ ASPxClientUtils.PreventEventAndBubble(e.htmlEvent); preventEndEditOnLostFocus = true;}}function C1spinEdit_LostFocus(s, e){if(!preventEndEditOnLostFocus) grid.batchEditApi.EndEdit(); preventEndEditOnLostFocus = false;}
See Also:
ASPxGridView - Batch Editing - A simple implementation of an EditItem template
GridView - Batch Editing - A simple implementation of an EditItemTemplate with client-side unobtrusive validation
Question Comments
Added By: Abdullah Garcia at: 5/14/2015 7:36:56 AM
Is there any example that uses GridLookup instead of a SpinEdit?
Added By: Mike (DevExpress Support) at: 5/15/2015 12:57:34 AMI see you've created a separate ticket with this subject. We'll reply to you there. Please bear with us.
Added By: Saitgalina Albina at: 3/30/2016 11:52:31 PM Hello! I do the same in my project, but the data is not saved. In HomeController.cs batchValues.Insert.count = 0, batchValues.Update.Coumt = 0, batchValues.DeleteKey.column = 0 always coming. What could be the reason? Added By: Mike (DevExpress Support) at: 3/31/2016 12:37:45 AM Hello,To process your recent post more efficiently, I created a separate ticket on your behalf: T362286: GridView in Batch Edit Mode - Why BatchValue Count = 0 on Controller. This ticket is currently in our processing queue. Our team will address it as soon as we have any updates.