This example demonstrates how to handle the BatchEditRowValidating event to validate duplicate values in batch edit mode.
Follow the steps below to enable this functionality:
-
Handle the grid's server-side CustomJSProperties event to get cell values from the server.
-
Merge the cloned server-side values and client-side changes to get unique values.
function GetUniqueColumnValuesForValidation(grid, columnIndex, editingRowKey, isModifying) { // Get the client-side changes var clientChanges = grid.batchEditApi.GetUnsavedChanges(); var uniqueValues = GetServerUniqueValues(grid)[columnIndex]; for (var rowKey in clientChanges.deletedValues) { var columnValue = clientChanges.deletedValues[rowKey][columnIndex]; delete uniqueValues[columnValue]; } for (var rowKey in clientChanges.insertedValues) { // ... var columnValue = clientChanges.insertedValues[rowKey][columnIndex]; uniqueValues[columnValue] = true; } for (var rowKey in clientChanges.updatedValues) { // ... var serverColumnValue = grid.batchEditApi.GetCellValueByKey(rowKey, columnIndex, true); var newColumnValue = clientChanges.updatedValues[rowKey][columnIndex]; delete uniqueValues[serverColumnValue]; uniqueValues[newColumnValue] = true; } return uniqueValues; }
-
Handle the grid's BatchEditRowValidating event to validate duplicate values. Use the validationInfo argument property to get information about the processed cell and call the
ValidateUniqueColumnValues
function. This function gets the new value and checks if this value duplicates one of the unique values.function OnBatchEditRowValidating(s, e) { for (var columnIndex in e.validationInfo) { var validationInfo = e.validationInfo[columnIndex]; // ... ValidateUniqueColumnValues(s, validationInfo, parseInt(columnIndex), e.key); } } function ValidateUniqueColumnValues(grid, validationInfo, columnIndex, rowKey) { // ... var newClientValue = validationInfo.value; // ... var uniqueValues = GetUniqueColumnValuesForValidation(grid, columnIndex, rowKey, isModifying); if (uniqueValues[newClientValue]) { validationInfo.isValid = false; validationInfo.errorText = "Duplicate value"; } }
(you will be redirected to DevExpress.com to submit your response)