FlexGrid for WinForms
Step 4 of 6: Add Data Validation
Show AllShow All
Hide AllHide All

If you assign a data type to a grid column, the grid will ensure that only data of the proper type is stored in that column. This helps prevent errors, but you will often need stricter validation to ensure that the data entered is correct. For that, you should use the ValidateEdit event.

For example, imagine that anti-trust regulations prevent us from selling our Applets in the North region. To prevent data-entry mistakes and costly lawsuits, we want to prevent users from entering this combination into the control.

The following event checks the data after each edit and prevents invalid entries. Add the following code to the form:

To write code in Visual Basic

To write code in C#

Run the program and observe the following:

The function starts by gathering the input that needs to be validated. Note that the values being checked are retrieved using the Editor.Text property. This is necessary because the edits have not yet been applied to the control. If the test fails, the function displays a warning and then sets the Cancel parameter to True, which cancels the edits and puts the cell back in edit mode so the user can try again.

Press F5 to run the project again, then try inputting some bad values. You will see that the control will reject them.


 

 


Copyright © GrapeCity, inc. All rights reserved.

Product Support Forum |  Documentation Feedback