Description:
How to Validate Changes Made to a Grid Cell
Answer:
There are two possible solutions to implement cell-based data validation. It is either using the RepositoryItem.Validating or GridView.ValidatingEditor event.
A) RepositoryItem.Validating
"sender" should be typecast to BaseEdit. Use the EditValue property to obtain the current value and check it. If the value is invalid, the e.Cancel parameter must be set to True.
B) GridView.ValidatingEditor
"sender" should be typecast to GridView. The current field name can be obtained from the GridView.FocusedColumn property. A value is passed as the e.Value parameter. If it is invalid, you should set the e.Valid parameter to False.
Then you should write a GridView.InvalidValueException event handler.
Here is some sample code:
C#private void gridView1_InvalidValueException(object sender, DevExpress.XtraGrid.Views.Base.InvalidValueExceptionEventArgs e) {
e.ThrowException = false;
e.WindowText = "The new value is invalid. Please correct it or press Esc to abandon your changes.";
e.DisplayError = true;
}
Visual BasicPrivate Sub GridView1_InvalidValueException(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.InvalidValueExceptionEventArgs) Handles GridView1.InvalidValueException
e.ThrowException = False
e.WindowText = "The new value is invalid. Please correct it or press Esc to abandon your changes."
e.DisplayError = True
End Sub
Please also review the "Handling Invalid Data" article in the Task Based help.
By the way, it's better to use the GridView.ValidatingEditor event instead of RepositoryItem.Validating to unify your validation code and have all it in the same place.
See Also:
How to display a custom error icon when an end-user enters invalid data into an editor