Handling Windows Forms Data Binding Errors
page 3 of 6
by Jon Kruger
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 33125/ 32

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.

View Entire Article

User Comments

Title: Easy open source alternative to data binding   
Name: Michael L Perry
Date: 2008-04-09 10:29:50 PM
Data binding can be complicated and error prone. Thanks for posting this quick guidance on making it simpler.

There is, however, and easier way. Please check out this open source alternative to data binding at http://codeplex.com/updatecontrols. Update Controls are Windows Forms controls that update themselves when your data changes. You don't have to bind the controls through a binding source. They can work with any business logic you want to use.

To use Update Controls, you don't bind to a property. Instead, you handle an event. For example, the UpdateTextBox control fires the GetText event to determine what string to display. You can call any business logic to figure out this string. The control pays attention to all of the data that you access during that event. When that data changes, the event is fired again. It's all automatic, and works with any business logic, no matter how complex or indirect.

Product Spotlight
Product Spotlight 

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2023 ASPAlliance.com  |  Page Processed at 2023-03-21 10:42:26 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search