How to automate ErrorProvider use in DataGridView

Topics: User Forum
Mar 18, 2008 at 2:14 PM
I have a form which along with some simple textbox controls, etc., also uses a DataGridView control. This DataGridView is bound to a BindingList which itself is a member property of the main business object to which the form binds to.

I would like to use the same error provider functionality I've used up to date with other controls, i.e. set the form's ErrorProvider's DataSource property to the business object I am binding to (decorated with appropriate validation rules). Is there extra behind the scenes code to make this happen with the DataGridView? Does the BindingList report per row errors as a result of binding to my business object which is derived from NotifyValidatableBase? I have ShowRowErrors set to true for the DataGridView control via the form designer. I haven't delved into the specifics of DataGridView error handling - is it the case I need to manually do this validation and setting error texts per cell/row in CellValidating events?

Mar 19, 2008 at 4:20 AM

I just did the following
  • opened up the basic windows form QuickStart (\Validation\QuickStarts\QSWindowsApplicationCSharp\BasicForm.cs)
  • placed a DataGridView on it
  • set the DataGridView data source to peronBindingSource
  • ran the QSWindowsApplicationCSharp project
  • cleared the first column (firstName)

An error message appears to so it seems like DataGridView works out of the box.

Am I missing something about what you are trying to achieve?

Mar 21, 2008 at 6:34 PM
I found what I was doing wrong. Really silly. In short, I had previously written my own base object for all my business objects that included a NotifyPropertyChanged method that was called in each properties Set method. As I transition over to this framework, not just for property change notification, but validation, I haven't been comprehensive in changing this call to NotifyAndValidate. I've been doing this as I make my way through each class, adding Rule attributes, etc.

What is the overhead if I just do a global search and replace on all my business objects and change NotifyPropertyChanged to NotifyAndValidate? For properties that don't have rule attributes, is there a cost to calling NotifyAndValidate vs. just NotifyPropertyChanged. It would be nice if there wasn't - I could then add Rules as needed and not worry about not catching this.

Simon, thanks for your vigilance in responding to the issues on the discussion board. It is very much appreciated.

Mar 22, 2008 at 12:25 PM

First off: The NotifyAndValidate method will throw an exception if no rules are found on a property. This is to help developers diagnose configuration issues. The workaround for this is to call one of the overloaded constructors of NotifyValidatableBase. It is the constructor with “ignoreNoRules” as the fourth param. This will mean that if a property is found with no rules it will not throw an exception.


There is an overhead associated with calling NotifyAndValidate. The question is if it will be noticeable for you situation. Below are some approximate timings.

On my machine 2.4GHz Core2 Duo 4GB of RAM

For 1,000,000 iterations

  • no rules, property changed and validate: 259 ms
  • one rule, property changed and validate: 262 ms
  • no rules, no property changed and no validate: 9 ms
  • no rules, property changed and no validate: 35ms

So, in summary, the overhead for a calling NotifyAndValidate (where there are no rules) is 0.000259ms per call.

What does this mean to you?

  • if you are doing a windows forms interface the overhead is insignificant
  • if you are doing a webservice or website with and extremely high load then the numbers become not so insignificant. But of course, if this is the case, you should not be doing Property change notification either :)

These are obviously easy timings to come up with. If you are truly concerned about performance for you app I recommend building performance counters into your unit tests.

If you give me some more details of what you are building I can give you some more detailed guidelines.


Mar 24, 2008 at 1:35 PM

Since I am doing a Windows Form interface (front end to display input/outputs of a scientific simulation), I think this it would be acceptable to do a search/replace on NotifyPropertyChanged to NotifyAndValidate so I don't have to worry about making the same mistake in thought throughout the rest of my gui as I integrate your framework.