Example T115116
Visible to All Users

GridView for ASP.NET MVC - How to cancel editing conditionally in batch edit mode

You can cancel grid data editing in batch edit mode on the client or server side.

In this example, users can modify data only in even rows (except the ID column). Each column is protected in a different way, listed below.

Implementation Details

On the client, handle the BatchEditStartEditing event and set its e.cancel property to true to cancel editing:

JavaScript
function OnBatchStartEdit(s, e) { if (condition) e.cancel = true; }

Another way to cancel editing is to use an editor's SetReadOnly method. The code sample below demonstrates how to disable an editor conditionally:

JavaScript
function OnBatchStartEdit(s, e) { if (condition) { editor = s.GetEditor(e.focusedColumn.fieldName); editor.SetReadOnly(true); }

You can implement custom server-side logic in the CustomJSProperties event handler:

C#
settings.CustomJSProperties += (s, e) => { var clientData = new Dictionary<int,object>(); var grid = s as MVCxGridView; for (int i = 0; i < grid.VisibleRowCount; i++) { var rowValues = grid.GetRowValues(i, new string[] {"ID", "ServerSideExample"}) as object[]; var key = Convert.ToInt32(rowValues[0]); if (key % 2 != 0) clientData.Add(key, "ServerSideExample"); } e.Properties["cp_cellsToDisable"] = clientData; };

Files to Look At

Documentation

More Examples

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

BatchEditCancel/Views/Home/Index.cshtml
Razor
<script type="text/javascript"> function OnBatchStartEdit(s, e) { //client processing var keyIndex = s.GetColumnByField("ID").index; var key = e.rowValues[keyIndex].value; var condition = key % 2 == 0; if (e.focusedColumn.fieldName == "ID") e.cancel = true; if (e.focusedColumn.fieldName == "ClientSideCancel") //cancel example if (!condition) e.cancel = true; if (e.focusedColumn.fieldName == "ClientSideReadOnly") { editor = s.GetEditor(e.focusedColumn.fieldName); // make read-only example editor.SetReadOnly(!condition); } //server preprocessing if (typeof s.cp_cellsToDisable[key] != "undefined" && s.cp_cellsToDisable[key] == e.focusedColumn.fieldName) e.cancel = true; } </script> @Html.Action("GridViewPartial")
BatchEditCancel/Views/Home/Index.vbhtml
Code
<script type="text/javascript"> function OnBatchStartEdit(s, e) { //client processing var keyIndex = s.GetColumnByField("ID").index; var key = e.rowValues[keyIndex].value; var condition = key % 2 == 0; if (e.focusedColumn.fieldName == "ID") e.cancel = true; if (e.focusedColumn.fieldName == "ClientSideCancel") //cancel example if (!condition) e.cancel = true; if (e.focusedColumn.fieldName == "ClientSideReadOnly") { editor = s.GetEditor(e.focusedColumn.fieldName); // make read-only example editor.SetReadOnly(!condition); } //server preprocessing if (typeof s.cp_cellsToDisable[key] != "undefined" && s.cp_cellsToDisable[key] == e.focusedColumn.fieldName) e.cancel = true; } </script> @Html.Action("GridViewPartial")
BatchEditCancel/Views/Home/GridViewPartial.cshtml
Razor
@Html.DevExpress().GridView(settings=>{ settings.Name = "gridView"; settings.CallbackRouteValues = new { Controller = "Home", Action = "GridViewPartial" }; settings.KeyFieldName = "ID"; settings.SettingsEditing.Mode = GridViewEditingMode.Batch; settings.CustomJSProperties += (s, e) => { var clientData = new Dictionary<int,object>(); var grid = s as MVCxGridView; for (int i = 0; i < grid.VisibleRowCount; i++) { var rowValues = grid.GetRowValues(i, new string[] {"ID", "ServerSideExample"}) as object[]; var key = Convert.ToInt32(rowValues[0]); if (key % 2 != 0) clientData.Add(key, "ServerSideExample"); } e.Properties["cp_cellsToDisable"] = clientData; }; settings.ClientSideEvents.BatchEditStartEditing = "OnBatchStartEdit"; }).Bind(Model).GetHtml()
BatchEditCancel/Views/Home/GridViewPartial.vbhtml
Code
@Html.DevExpress().GridView( Sub(settings) settings.Name = "gridView" settings.CallbackRouteValues = New With {.Controller = "Home", .Action = "GridViewPartial"} settings.KeyFieldName = "ID" settings.SettingsEditing.Mode = GridViewEditingMode.Batch settings.CustomJSProperties = Sub(s, e) Dim clientData = New Dictionary(Of Integer, Object)() Dim grid = TryCast(s, MVCxGridView) For i As Integer = 0 To grid.VisibleRowCount - 1 Dim rowValues = TryCast(grid.GetRowValues(i, New String() {"ID", "ServerSideExample"}), Object()) Dim key = Convert.ToInt32(rowValues(0)) If key Mod 2 <> 0 Then clientData.Add(key, "ServerSideExample") End If Next e.Properties("cp_cellsToDisable") = clientData End Sub settings.ClientSideEvents.BatchEditStartEditing = "OnBatchStartEdit" End Sub).Bind(Model).GetHtml()

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.