LogoASPAlliance: Articles, reviews, and samples for .NET Developers
Handling Windows Forms Data Binding Errors
by Jon Kruger
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 33434/ 85


Data binding is great, but one thing that newbies do not know is that data binding will eat exceptions and never tell you - unless you know what events to listen to.

Back in the .NET 1.1 days, this was really bad because there was no way to find out that these exceptions were happening. So you would sit there for hours wondering why things were not working when everything looked fine.

What can go wrong?

There are a few different things that can go wrong when you use data binding.

·         Exception when setting the value

·         Value assigned to the property fails validation (but does not throw an exception)

·         Exception thrown when data binding cannot call the setter

·         Exception thrown by someone handling a CurrencyManager event

What can I do about it?

If you use the BindingSource component to do data binding, then this will not be too hard. If you are not using the BindingSource component, then I would highly recommend that you do because otherwise things are going to be a lot more complicated and you might have to handle a lot more events.

Here are the two events that you need to be concerned with (all on the BindingSource component):

·         BindingComplete - This fires when a binding operation is completed (that is, after it is done trying to push the value into the property). The event args have a BindingCompleteState property that returns one of three states: Success (if everything went OK), DataError (if it is able to push the value into the property but the value failed validation), and Exception (if an exception was thrown for any reason).

·         DataError - This event will report exceptions that happen during CurrencyManager events (usually because someone handles an event like CurrencyManager.CurrentChanged and does something that causes an exception).

Do not confuse the DataError event with the BindingCompleteState.DataError value in BindingComplete. You might think that the DataError event will fire when a value fails validation, but this is not the case.

If an exception occurs in the setter of the property (or something called by the setter) or if there is an exception thrown by the reflection code that is trying to call the setter, you might see odd behavior in your controls, such as your control not letting you change focus to anything else. If you ever see this behavior and you are not sure why, handle the BindingSource.BindingComplete event and you will probably find out what is going on.

It is not a bad idea to handle the BindingComplete and DataError events just so that you can be aware when these exceptions occur, even if it is just for debugging purposes. Otherwise, bad things could be happening and you might not know it.

Tips for using grid controls


Grid controls work a little bit differently. For example, the DataGridView exposes a DataError event, which reports information about exceptions that happen during data binding. If you do not handle the DataError event, the grid will pop up a message box explaining that an exception occurred and that you should handle the DataError event. At least this way you know if something went wrong.


In this article you have learned how to handle some of the data binding errors in Windows Forms applications.




Product Spotlight
Product Spotlight 

©Copyright 1998-2021  |  Page Processed at 2021-10-27 4:00:31 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search