Databinding and error validation in WinForm

image

I write this post because I have searched a long time before to obtain an acceptable result with error validation and databinding.

With databinding and a little bit of code you can obtain the result from the screen shot rapidly and this code is very stable.

The first step is to write our business object, in this sample the class Person with 2 properties "Lastname" and "Firstname".

Next via the datasource window we drag and drop the first name and last name property into the window. The error property in the following screen shot will be explained later.

image

Next to show errors on our winform we add an error provider to the window and we can set the current error for each control via the validate event of each control but it's long and it's for each new form with the same object.

The error provider component have a property datasource, we can bind to it a dataset and it will show errors into data grid but for custom object?

You have to implement the interface IDataErrorInfo. This interface have 2 get properties Error and an indexer with a string as key. The error property return a message saying if the whole object have one or more errors, the indexer return for a property if the property is good or not. When a bound control lose focus, it will read the indexer property and the string parameter will be the property name to check, if we return a string different of null or empty, the property is in error state and the error provider will show it.

See the implementation of the Person class:

 

   1 using System;

    2 using System.Collections.Generic;

    3 using System.Text;

    4 using System.ComponentModel;

    5 

    6 namespace DataBindingTest

    7 {

    8     class Person : IDataErrorInfo

    9     {

   10         private Dictionary<string, string> errors = new Dictionary<string, string>();

   11 

   12         private string error = string.Empty;

   13 

   14         private string lastname;

   15         private string firstname;

   16 

   17         private void Validate(string field, string value)

   18         {

   19             switch (field)

   20             {

   21                 case "Lastname":

   22                     if (String.IsNullOrEmpty(value))

   23                     {

   24                         this.errors["Lastname"] = "There is an error";

   25                     }

   26                     else

   27                     {

   28                         this.errors.Remove("Lastname");

   29                     }

   30                     break;

   31                 case "Firstname":

   32                     if (String.IsNullOrEmpty(value))

   33                     {

   34                         this.errors["Firstname"] = "There is an error";

   35                     }

   36                     else

   37                     {

   38                         this.errors.Remove("Firstname");

   39                     }

   40                     break;

   41                 default:

   42                     break;

   43             }

   44         }

   45 

   46         public string Lastname

   47         {

   48             get

   49             {

   50                 this.Validate("Lastname", this.lastname);

   51                 return this.lastname;

   52             }

   53 

   54             set

   55             {

   56                 this.lastname = value;

   57                 this.Validate("Lastname", this.lastname);

   58             }

   59         }

   60 

   61         public string Firstname

   62         {

   63             get

   64             {

   65                 this.Validate("Firstname", this.firstname);

   66                 return this.firstname;

   67             }

   68 

   69             set

   70             {

   71                 this.firstname = value;

   72                 this.Validate("Firstname", this.firstname);

   73             }

   74         }

   75 

   76         #region IDataErrorInfo Members

   77 

   78         public string Error

   79         {

   80             get

   81             {

   82                 if (this.errors.Count > 0)

   83                 {

   84                     return "There are " + this.errors.Count + " errors";

   85                 }

   86 

   87                 return null;

   88             }

   89         }

   90 

   91         public string this[string columnName]

   92         {

   93             get

   94             {

   95                 if (this.errors.ContainsKey(columnName))

   96                 {

   97                     return this.errors[columnName];

   98                 }

   99                 else

  100                 {

  101                     return null;

  102                 }

  103             }

  104         }

  105 

  106         #endregion

  107     }

  108 }

 

This method is good for basic validation such as null, length, regex validation.

Now if you change the property of our custom object via business code, it will not be reflected directly to the screen. The binding source doesn't know that the value has changed. The solution is to implement the interface INotifyPropertyChanged, it will declare the event PropertyChanged on your class. Then you raise this event giving to it the name of the property into the setter of all of the properties of the Person class and now the binding source will be notified of the change and refresh it on the screen.

61 public string Firstname

   62         {

   63             get

   64             {

   65                 this.Validate("Firstname", this.firstname);

   66                 return this.firstname;

   67             }

   68 

   69             set

   70             {

   71                 this.firstname = value;

   72                 if (this.PropertyChanged != null)

   73                 {

   74                     this.PropertyChanged(this, new PropertyChangedEventArgs("Firstname"));

   75                 }

   76                 this.Validate("Firstname", this.firstname);

   77             }

   78         }

There are a lot of interfaces you can implement to have your live easier...







1 comments:
gravatar
Anonymous said...
December 29, 2012 at 4:40 PM  

rofl what dumb sob posted this crapola article.\who ever posted this shit has unit inch up homos ahole

Post a Comment