Stopping "automatic" updating of controls with datasource

Grayda

Member
Joined
May 20, 2010
Messages
16
Programming Experience
10+
I'm writing an app that pulls information from a DataSet as a one-way thing (e.g. the datasource populates a listbox, but for selection purposes only -- the user cannot change the list values). My problem is, when I select something from the listbox, all of the other listboxes change too, as if the control is telling the others "Update your contents to display what is in row 5"

Say my dataset looks like this (dataset > table > column > row value):

VB.NET:
datInfo
  tblBlah
    colOne
      One One
      One Two
      One Three
    colTwo
      Two One
      Two Two
      Two Three
      Two Four

I then use the IDE to bind lstOne and lstTwo (both listboxes) to datInfo, and use the DisplayMember to bind lstOne to colOne and lstTwo to colTwo.

When I run and click on a value in one listbox, the other one follows suit. For example if I click on "One Three" in lstOne, lstTwo displays "Two Three". I expect to click on "One Three" then be able to independently set lstTwo to "Two One"

This seems counter-intuitive, so is there a way to do what I'm after from the IDE, or will I have to do it all from code?
 
What you are seeing is by design and it's a good thing too. Consider the situation where you bound one column to a ListBox and the other to a TextBox, so that the user could edit the second field. When you made a selection in the ListBox, wouldn't you want the TextBox to display the data from that selected record? The situation where you want to bind one list to more than one control and make independent selections is the far less common. As such, it's the one that requires you to do things differently. It's still very easy though.

At the moment, when you bind the same DataTable to both controls, only one CurrencyManager is created. A CurrencyManager is what handles most of the work associated with data-binding behind the scenes. You need a separate CurrencyManager for each control. There are two good ways to do that:

1. Create two new DataViews from your DataTable and bind them to the controls instead of the DataTable itself.
2. Add two BindingSources to your form, bind the DataTable to them and bind them to the controls.

I would go with the second option. In both cases though, you are binding each control to a different source, so each one creates its own CurrencyManager.
 
Hadn't really paid attention to the BindingSource control before. Makes perfect sense, now that you mention it. I was half expecting a property you could set "BindToRow" or something, but this is acceptable.

I suppose if my form has twenty fields to propagate, I'd need twenty instances of this control? Would probably be quicker / easier to code them in, if that is the case. Fortunately my app only requires a small handful so this is ideal.

Thanks!
 
Back
Top