Binding 2 ComboBoxes within the same form to a single data source
-
Hi, I'm sure I have seen this question posted before on CP but I am unable to find it if so. I have 2 ComboBoxes on a form both bound to the same data source. But when I change the selected index in the first the second changes also, and vise-versa. Can somebody please give me some hints oh how to do this properly. Thanks. (Just in case it matters, the data source is a generic list of user defined objects)
-
Hi, I'm sure I have seen this question posted before on CP but I am unable to find it if so. I have 2 ComboBoxes on a form both bound to the same data source. But when I change the selected index in the first the second changes also, and vise-versa. Can somebody please give me some hints oh how to do this properly. Thanks. (Just in case it matters, the data source is a generic list of user defined objects)
<EDIT> Sorry - I just reread that and it didn't sound right. It should be BindingSource, not Navigator. Attribute it to rectal-cranial inversion. </EDIT> You have to stick 2 BindingSources's in between the combos and the datasource. Each combo will have it's own BindingSource between it and the datasource. Both BindingSources will have their DataSource properties pointing at the single data source.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009...modified on Thursday, July 30, 2009 12:19 PM
-
<EDIT> Sorry - I just reread that and it didn't sound right. It should be BindingSource, not Navigator. Attribute it to rectal-cranial inversion. </EDIT> You have to stick 2 BindingSources's in between the combos and the datasource. Each combo will have it's own BindingSource between it and the datasource. Both BindingSources will have their DataSource properties pointing at the single data source.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009...modified on Thursday, July 30, 2009 12:19 PM
OK Dave thanks for that :) EDIT: Just tried it and it worked perfectly. Should I be using the BindingSource object to do all of my data binding rather than binding directly to the data source?
modified on Friday, July 31, 2009 4:19 AM
-
OK Dave thanks for that :) EDIT: Just tried it and it worked perfectly. Should I be using the BindingSource object to do all of my data binding rather than binding directly to the data source?
modified on Friday, July 31, 2009 4:19 AM
Not really. I only use it when I've got multiple controls that need to see and navigate a single datasource independant of other controls. Think of it this way. Every control on a form using the same datasource also uses the same binding manager. The binding manager maintain a currency manager (which has nothing to do with money!) which keeps track of which record is the current record all of the controls are looking at. When one control changes the current record, all the controls get pointed at the new current record. The BindingSource class lets you escape that model and sets up another, seperate set of binding objects that does the same thing, independant of the first, or default set. In your case, technically, you don't need two BindingSource objects. You only need one. I just used two objects for better code readability. What's easier to understand??
Dim bs As New BindingSource bs.DataSource = data ComboBox1.DataSource = data ComboBox2.DataSource = bs
or
Dim bs1 As New BindingSource Dim bs2 as New BindingSource bs1.DataSource = data bs2.DataSource = data ComboBox1.DataSource = bs1 ComboBox2.DataSource = bs2
They work exactly the same...
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009... -
Not really. I only use it when I've got multiple controls that need to see and navigate a single datasource independant of other controls. Think of it this way. Every control on a form using the same datasource also uses the same binding manager. The binding manager maintain a currency manager (which has nothing to do with money!) which keeps track of which record is the current record all of the controls are looking at. When one control changes the current record, all the controls get pointed at the new current record. The BindingSource class lets you escape that model and sets up another, seperate set of binding objects that does the same thing, independant of the first, or default set. In your case, technically, you don't need two BindingSource objects. You only need one. I just used two objects for better code readability. What's easier to understand??
Dim bs As New BindingSource bs.DataSource = data ComboBox1.DataSource = data ComboBox2.DataSource = bs
or
Dim bs1 As New BindingSource Dim bs2 as New BindingSource bs1.DataSource = data bs2.DataSource = data ComboBox1.DataSource = bs1 ComboBox2.DataSource = bs2
They work exactly the same...
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009...Ah I see, understood :) Thanks for taking the time to explain it so well.