AUTHOR: Desislava Yordanova
DATE MODIFIED : September 25, 2017
DATE POSTED: March 28, 2014
When RadGridView is in bound mode, modifying a certain cell, automatically updates the DataBoundItem, which is the desired by design behavior. RadGridView provides a convenient way to realize data validation before it is committed to the underlying data source.
To achieve data validation prior the row is committed, you can handle the CellValidating event, which is raised by the control when the current cell changes or when it goes out of edit mode. Canceling this event prevents the user from exiting the cell until a valid value is entered or the edit process is canceled (via Esc key). The DataBoundItem will be updated only if the cell is validated.
However, the user may prefer to validate all the data cells in a row at once. This scenario can be achieved by using the RowValidating event, which is fired when the user tries to change the current row (changing to another row or when the control loses focus). Note that only the CellValidating event can keep the editor active,when the validation fails and it does not allow the user to exit the cell until a valid value is filled. However, pressing the Esc key in this situation will revert the initial value of the cell and it will close the editor. Thus, the user can continue. The RowValidating event has a slightly different behavior. When the validation fails, the editor is closed, but the user cannot change the current row in the grid until a valid cell value is entered. In this situation the editor is not kept active when the validation fails and pressing the Esc key does not revert the changes as the editor is already closed. This funtionality can be easily implemented by handling the PreviewKeyDown event, which will revert the row changes after pressing the Esc key.
Let's assume that the RadGridView is bound to a list of custom objects. We have subscribed to the RowValidating event in order to perform data validation on row level.
sender, RowValidatingEventArgs e)
"Empty value is not allowed"
, e AsRowValidatingEventArgs)
If the user enters empty string for a specific cell and tries to navigate to another row, the validation fails and the ErrorText is displayed on the corresponding row. At this point the user is not allowed to cancel the performed changes, pressing the Esc key.
To achieve this, we will have to subscribe to the PreviewKeyDown event:
//Cell index as a Key, cell value as Value
sender, PreviewKeyDownEventArgs e)
GridViewDataRowInfo && e.KeyData == Keys.Escape)
//revert the previous cell value which is stored in advance.
> cell ininitialValues)
.radGridView1.CurrentRow.Cells[cell.Key].Value = cell.Value;
'Cell index as a Key, cell value as Value
, e AsPreviewKeyDownEventArgs)
.radGridView1.CurrentRow IsGridViewDataRowInfoAndAlso e.KeyData = Keys.Escape
'revert the previous cell value which is stored in advance.
.radGridView1.CurrentRow.Cells(cell.Key).Value = cell.Value
This will mark that validation as unsuccessful. You can use the CurrentRowChanged event to make a new instance of the dictionary, in which the initial row data is stored:
sender, CurrentRowChangedEventArgs e)
, eAs CurrentRowChangedEventArgs)
The CellValidating event is used to extract the initial row data:
sender, CellValidatingEventArgs e)
cellIndex = e.ColumnIndex;
initialCellValue = e.Row.Cells[cellIndex].Value;
, e AsCellValidatingEventArgs)
When the row is successfully validated, the RadGridView.Tag property is cleared:
sender, RowValidatedEventArgs e)