KB Article A289
Visible to All Users

How to Validate Changes Made to a Grid Cell

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 Basic
Private 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

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.