Click here to Skip to main content
15,885,899 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
connection.Open();
string query = "select * from product_details";
OleDbDataAdapter da = new OleDbDataAdapter(query, connection);
DataSet ds = new DataSet();
da.Fill(ds, "product_details");
 
combo1.DataSource = ds.Tables["product_details"];
combo1.DisplayMember = "barcode";
combo1.ValueMember = "product_name";

 
combo2.DataSource = ds.Tables["product_details"];
combo2.DisplayMember = "product_name";
combo2.ValueMember = "barcode";
Posted
Comments
BillWoodruff 25-Feb-15 0:42am    
Unclear: 1. it appears you insert two ComboBoxes and a TextBox into one row of a TableLayoutPanel ... but you say "If I have 2 or more rows ..."

2. " all the Comboboxes change at the same time." What does this mean ? If both ComboBoxes are data-bound to the same DataSource, you would expect their contents to synchronize with any change in the DataSource ... yes ? Do you mean something else by "change" ?
Peter M. Adeeb 25-Feb-15 15:21pm    
the rows added dynamically .
each row has two combobox (combo1 , combo2 )
the problem (if i select an item from combo1 in any row all combo1 in other rows display the same item) the same thing for combo2 .
i want each row doesn't effected by other rows .
sorry for bad English , :D thanks in advance
BillWoodruff 1-Mar-15 21:01pm    
Sorry for the delay in responding, been busy. If you want the ComboBoxes on each Row to synchronize only with each other, then the ComboBoxes on each Row must have separate DataSources.

If that's not helpful, then I don't understand what you are describing.

1 solution

First of all it is not the problem of TableLayoutPane nor even dynamic combobox rather problem of datasource you've chosen.This problem has been created because of use same datasource.
Normally if you use List  as  Combobox datasourcs.however to overcome this you can use
a simple dictionary and it won't behave so.Therefore you may use Dictionary as you Combobox datasource so that one combobox cannot mimic the other.Sample given below:

C#
public Form1()
      {
          InitializeComponent();

          Dictionary<string,string> dicProduct = new Dictionary<string,string>();
          Product p1 = new Product();
          p1.ProductName = "AAA";
          p1.barCode = "092132313";
          Product p2 = new Product();
          p2.ProductName = "AAB";
          p2.barCode = "092132312";
          Product p3 = new Product();
          p3.ProductName = "AAC";
          p3.barCode = "092132311";
          Product p4 = new Product();
          p4.ProductName = "AAD";
          p4.barCode = "092132310";

          dicProduct.Add(p1.barCode, p1.ProductName);
          dicProduct.Add(p2.barCode, p2.ProductName);
          dicProduct.Add(p3.barCode, p3.ProductName);
          dicProduct.Add(p4.barCode, p4.ProductName);



          ComboBox combo1 = new ComboBox();
          ComboBox combo2 = new ComboBox();



          combo1.DataSource = new BindingSource(dicProduct,null);
          combo1.DisplayMember = "Value";
          combo1.ValueMember = "key";

          combo2.DataSource = new BindingSource(dicProduct, null);
          combo2.DisplayMember = "Key";
          combo2.ValueMember = "Value";

          tableLayoutPanel1.Controls.Add(combo1, 0, 0);
          tableLayoutPanel1.Controls.Add(combo2, 1, 0);




      }

      class Product
      {
          public string ProductName { get; set; }
          public string barCode { get; set; }
      }

Hope this helps!happy Coding! :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900