Datagridview validating input

If Has Errors is set to true, then you must iterate over the data set's Tables collection to determine which table has data with errors by checking the Has Errors Boolean property of each table.

Data-bound controls raise events when validation occurs, giving the opportunity to write validation logic.

The Validating event is the one that handles most of the validation logic.

This event defines an event argument (Cancel Event Args) which contains the Boolean property named Cancel to set a signal that the event being fired should not be completed.

A problem with this approach is that if the users enter an invalid value and tries to close the application, by default, they won't be able to do it. NET 2.0, we have the Auto Validate property on the Form class that lets you specify exactly what the behavior should be when the validation error occurs at the control level.

The Auto Validate property may have four values: Disable, Enable Prevent Focus Change, Enable Allow Focus Change, Inherit. If you set Enable Prevent Focus Change value and Cancel is set to true, focus will remain on the control that failed the validation.

There is a deep discussion about when, where and how this validation should be done.

You must have an account to comment. Please register or login here!