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:
JavaScriptfunction 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:
JavaScriptfunction 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
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")
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")
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()
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()