My Software Notes

Useful things I discover

Updating a DataSource that is bound to a Windows Forms Control

leave a comment »

Do you ever get that annoying problem where you have bound your business object to some controls on a Windows Form and yet after you make a change to the data in a control, your change is not moved to the underlying data source?

I had that happen today, but only when I had made the change through code and then clicked on the Okay button immediately after.  The change I’d made just before clicking the button was not put into the business object.

I had, of course, put a BindingSource between the business object and the controls.  You should always use a BindingSource because it provides the plumbing you need to really make data binding work.  I had used the Visual Studio designer to hook the controls to the BindingSource.  Everything seemed right, but I wasn’t getting my updates.

The problem I was hitting was that the code generated to create the DataBinding between the control property (in this case Text) and the underlying business object property (in this case DestinationRoot) was defaulting to a DataSourceUpdateMode of OnValidation:

    new Binding("Text", this.bindingSourceOptions, "DestinationRoot", true,

What happened was that I made the change in the text box through code, clicked Okay, and the dialog closed. OnValidation didn’t fire because the control never had focus, so the change I made never go into my business object.  What I needed was an update mode of OnPropertyChanged. So I went into the generated code and change it to … just kidding.  (I have actually seen someone do that.)

I used the VS08 IDE.  Click on the control you want to fix the bindings on and open the handy-dandy Properties window. At the top (if you sort it alphabetically) there is a “(DataBindings)” property that you can expand. The most common properties to bind to are listed but there is also an “(Advanced)” property and that’s what you want to use. Click on the “…” button and the “Formatting and Advanced Binding” dialog opens. You’ll find that EVERY property of the control that can be bound to is listed there. The dialog lets you select the control’s Property, the DataSource to bind to, how you want to format the displayed value AND a combo called “Data Source Update Mode”, which is where you can change when your underlying data gets updated.

I made the change on my text box and the generated code looked like this (I took out the namespaces the designer loves to put in to make your lines of code 15 feet long):

    new Binding("Text", this.bindingSourceOptions, "DestinationRoot", true,

Now my dialog box works perfectly.

If you are having troubles in the opposite direction, that is you update your underlying data and your UI controls don’t reflect the change, then you need to implement the INotifyPropertyChanged Interface.


Written by gsdwriter

March 19, 2009 at 1:29 pm

Posted in Languages, Tools, UI

Tagged with , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: