|
-
April 24th, 2008, 01:49 PM
#1
.NET DataBinding oddity
So I'm playing with a screen in our system. On the screen are a few comboboxes that we have databound to a list of stuff from a cache. Dictionary type stuff that we don't have to go to the database for everytime we need the list. Also, two of the comboboxes use the same list as it's datasource. No big deal right?
Well, whenever I set the list explicitly to ComboBox.DataSource, changing the value in one combobox changes the other one too. Before you say it, I've checked a thousand times and the SelectedItem of the binding are set to the two different properties on my business object. The DisplayMember and ValueMember are also set correctly. The selected values of the two comboboxes are getting set in the BO correctly, but they aren't being displayed at all. And, like I said, changing the value in one combobox changes the other.
So, as a test I set the collection into a BindingSource then set the BindingSource as the ComboBox.DataSource. One separate BindingSource for each combobox:
Code:
BindingSource tempBS1 = new BindingSource();
tempBS1.DataSource = Cache.Officers;
cboCollectionOfficer.DataSource = tempBS1;
BindingSource tempBS2 = new BindingSource();
tempBS2.DataSource = Cache.Officers;
cboGradeOfficer.DataSource = tempBS2;
When done this way, the comboboxes behave correctly. They each have their own separate values and they display and save correctly.
Can anyone out there smarter and better than me at databinding tell me why this is? It just doesn't make sense to me changing one combobox would change the other, because all they share is the collection of values to populate in the combobox itself, not the property on the business object where the value comes from. I'm pretty boggled over here!
-
April 24th, 2008, 02:06 PM
#2
Re: .NET DataBinding oddity
Self reply inc!
http://weblogs.asp.net/grobinson/arc.../11/27127.aspx
Seems this individual has the same problem and essentially the same workaround.
One of the comments:
I actually figured it out. When you set the DataSource property on a control. behind the scenes, a binding type is created. So, in our case, all 4 comboboxes are bound to the same datasource so of course they will stay in synch. My work around was to get a copy of the Datasource for each control.
The CurrencyManager only keeps the Position for one instance of the bound collection, so that's why changing one affected the other.
Last edited by opedog; April 24th, 2008 at 02:08 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|