且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Windows窗体C#中使用TableLayoutPanel和动态Combobox

更新时间:2022-10-23 11:38:51

首先,它不是TableLayoutPane的问题,甚至也不是动态组合框,而是您选择的数据源问题。这个问题已经创建因为使用相同的数据源。 
通常如果你使用List作为Combobox datasourcs.however来克服这个问题,你可以使用
a简单字典而且它不会表现如此。因此你可以使用Dictionary作为你的Combobox数据源,这样一个组合框就无法模仿其他。下面给出的示例:



  public  Form1()
{
InitializeComponent();

字典<字符串,字符串> dicProduct = new Dictionary< string,string>();
产品p1 = 产品();
p1.ProductName = AAA;
p1.barCode = 092132313;
产品p2 = 产品();
p2.ProductName = AAB;
p2.barCode = 092132312;
产品p3 = 产品();
p3.ProductName = AAC;
p3.barCode = 092132311;
产品p4 = 产品();
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 产品
{
public string ProductName { get ; set ; }
public string barCode { get 跨度>; set ; }
}



希望这有帮助!快乐编码! :)


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";

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:


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! :)