Comboboxes are linked for some reason

后端 未结 1 1282
旧时难觅i
旧时难觅i 2020-11-29 13:54

I have the following code to populate 3 comboboxes:

private void PopulateDDLs()
{
    SqlConnection connection;
    SqlCommand command;
    SqlDataReader rea         


        
相关标签:
1条回答
  • 2020-11-29 14:46

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

    0 讨论(0)
提交回复
热议问题