1

I have the following code to populate 3 comboboxes:

private void PopulateDDLs()
{
    SqlConnection connection;
    SqlCommand command;
    SqlDataReader reader;
    DataTable dt;

    using (connection = new SqlConnection("connection string here"))
    {
        using (command = new SqlCommand("sql query here", connection))
        {
            connection.Open();
            using (reader = command.ExecuteReader())
            {
                dt = new DataTable();
                dt.Load(reader);

                ddl1.ValueMember = "col1";
                ddl1.DisplayMember = "col2";
                ddl1.DataSource = dt;

                ddl2.ValueMember = "col1";
                ddl2.DisplayMember = "col2";
                ddl2.DataSource = dt;

                ddl3.ValueMember = "col1";
                ddl3.DisplayMember = "col2";
                ddl3.DataSource = dt;
            }
            connection.Close();
        }
    }
}

However, when I execute this program, and make a selection from one of the comboboxes, the same value automatically gets selected from the other comboboxes. Any idea why this is happening and how to stop it from happening more importantly?

If I create 3 functions, 1 for each combobox, then everything works fine.

This project is a Word Document level project for Word 2010 created using .NET-4.0 using VS2013.

oshirowanen
  • 15,297
  • 82
  • 198
  • 350
  • It looks like some more info is needed in the question: - Do the comboboxes auto-post? - you wrote you have 3 comboboxes, but then: "If I create 2 functions, 1 for each combobox". Did you mean 3 functions, by any chance? – H.Wolper May 27 '14 at 10:32
  • @AngelEyes, yes sorry, should have been 3, and I've added a note to say that the project is a Word Document project created with VS2013. No auto-posts. – oshirowanen May 27 '14 at 11:20
  • weird. i would expect it to work, what happens if you use `ddl2.DataSource = dt.Clone();` ? – Koryu May 27 '14 at 12:45

1 Answers1

4

New much improved solution:

A DataSource is more than just the data.

There is hidden default BindingSource that makes the ComboBoxes follow.

To avoid this coupling and also the data replication in the first version of this answer, all you need to do is create a separate BindingSource for each ComboBox. These share the DataTable but have each its own rowPointer:

BindingSource bS1, bS2, bS3;
..
..    
..
..    
dt = new DataTable();
dt.Load(reader);

bS1 = new BindingSource();
bS1.DataSource = dt; 
bS2 = new BindingSource();
bS2.DataSource = dt; 
bS3 = new BindingSource();
bS3.DataSource = dt;
..
ddl1.DataSource = bS1 ;
ddl2.DataSource = bS2 ;
ddl3.DataSource = bS3 ;
..
..

Now the ComboBoxes can be changed independently.

Note: My first version worked but was the wrong way do it. Sorry..!

TaW
  • 53,122
  • 8
  • 69
  • 111
  • When you bind all combo boxes to the same data source they share a single `BindingManagerBase` and when you choose an item from one of combo boxes, the current `Position` of the shared binding manager base changes and all combo boxes goes to that position of their shared data source. Here is a [similar question](https://stackoverflow.com/q/35864906/3110834) suggesting some other solutions as well. For a `DataTable`, using `this.comboBox1.DataSource = new DataView(dt);` will also do the trick, it's equivalent to `ToList` solution in the linked post. – Reza Aghaei Apr 09 '18 at 19:37
  • The Bindingcontext is interesting when several controls nested in a container shall share their rowpointer. The ToList() creates a copy of the data; sometimes just what is needed but usually just expensive. So many tools in our toolboxes :-) – TaW Apr 09 '18 at 19:46