Welcome to FoxLearn
In this video, I'll show you how to validate user input using the Data Annotation Validator Attributes and Error Provider in C# Windows Forms Application
Open your Visual Studio, then create a new Windows Forms Application project
Next, Right-click on your project, then add a reference to the System.ComponentModel.DataAnnotations library.
We'll create a Customer class and decorate its properties with validation attributes to practice
how to use the Data Annotation Validator Attributes and ErrorProvider to perform validation in a Windows Forms Application.
The advantage of using the Data Annotation validators is that they enable you to perform validation simply by adding one or more attributes.
Such as the Required, Range, Regular Expression or StringLength attribute to a class property.
The Required attribute enables you to mark a property as required.
The Range attribute enables you to validate whether the value of a property falls between a specified range of values.
Next, Rebuild your project, then add a DataSource to your form.
You can design a simple UI that allows you to enter customer information
The Validation Context class describes the type or member on which validation is performed.
It also enables custom validation to be added through any service that implements the Service Provider interface.
The Validation Result Class represents a container for the results of a validation request
As you can see, we can easily validate user input using the Data Annotation Validator Attributes
The Regular Expression attribute enables you to validate whether the value of a property matches a specified regular expression pattern.
This is the easiest way to validate First Name and Last Name
We will create an EntityValidator class to help us use ErrorProvider with DataAnnotations Validation Attributes
To show validation errors in Windows Forms, you can use the ErrorProvider component.
The .NET framework also supports showing error in DataGridView.
To be able to show model errors in the DataGridView or showing errors using ErrorProvider,
the model which you are using in data binding should implement IDataErrorInfo.
For example DataRowView which is the main model which is usually used in classic ADO.NET applications, has implemented IDataErrorInfo.
To bring validation attributes support to our model classes, we need to implement IDataErrorInfo.
There is a Validator class in the System.ComponentModel.DataAnnotations library which has a couple of methods
which allows us to validate an object or a property of an object using validation attributes
You will need to override the Indexer, the Indexer get the error message for the property with the given name.
You also need to override the Error property, the Error property get an error message indicating what is wrong with this object.
Next, We will drag a DataGridView from the Visual Studio toolbox into our form designer,
and then add the DataSource to the DataGridView control
As you can see, the Error Provider automatically shows up in the DataGridView when an In Valid data error occurs.
Thank you for watching this video and don't forget to subscribe to my channel