Windows Combobox Controls that share the same DataSource

We have a states Datatable (50 US States) that has 2 DataColumns, 'code', which equals the state abbreviation, and 'codedesc', which equals the state full name.  We retrieve the data for the DataTable from SQL Server at app start-up and cache it for later use. 

We have 4 combobox controls on one form that all need to display states.  So, we share the cached states DataTable as their DataSource.  We also databind the control's SelectedValue property to another datasource. 

Databinding cannot keep the states in synch for some reason when sharing the cached states DataSource.  Navigation display all kinds of different states in the 4 controls, but never the correct ones.  

So, we call copy on the cached states DataTable 3 times, meaning we now have 4 states DataTables (the original plus 3 copies) .  We then set states1 combobox to the original Datatable, states2 to copy1, states3 to copy2 and states4 to copy3.  DataBinding is now happy and everything is in synch.  The only difference is the combobox controls no longer share, or use, the same DataSource.  They use a unique DataSource where the DataSource is identical.   

Can anyone explain why sharing a DataSource for an IList control does not work when DataBinding? 

5 Comments

  • Greg

    Is something similar to streams happening here, where you read until the end and have to reset the current position before another routine could read off the same stream? Just wondering...

  • 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.

  • Try creating a Dataview for each one, rather then tying the same DataTable to 4 dropdowns.

  • I do understand your idea. But can you please show me the ways to make a copy of the DataSource.

    Sim, I do not really understand what is DataView!

  • I had the same problem. After some time of googling I found:

    "If you have two controls bound to the same datasource, and you do not want them to share the same position, then you must make sure that the BindingContext member of one control differs from the BindingContext member of the other control. If they have the same BindingContext, they will share the same position in the datasource.

    If you add for example a ComboBox and a ListBox to a form, the default behavior is for the BindingContext member of each of the two controls to be set to the Form's BindingContext. Thus, the default behavior is for the ListBox and ComboBox to share the same BindingContext, and hence the selection in the ComboBox is synchronized with the current row of the ListBox. If you do not want this behavior, you should create a new BindingContext member for at least one of the controls.

    comboBox1.BindingContext = new BindingContext();"

    details there: http://www.akadia.com/services/dotnet_unshare_datasource.html

Comments have been disabled for this content.