在Windows窗体C#中使用TableLayoutPanel和动态Combobox [英] using TableLayoutPanel and dynamic Combobox in windows form C#

查看:112
本文介绍了在Windows窗体C#中使用TableLayoutPanel和动态Combobox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 connection.Open(); 
string query = select * from product_details ;
OleDbDataAdapter da = new OleDbDataAdapter(查询,连接);
DataSet ds = new DataSet();
da.Fill(ds, product_details);

combo1.DataSource = ds.Tables [ product_details];
combo1.DisplayMember = 条形码;
combo1.ValueMember = product_name;


combo2.DataSource = ds.Tables [ product_details];
combo2.DisplayMember = product_name;
combo2.ValueMember = 条形码;

解决方案

首先,它不是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! :)


这篇关于在Windows窗体C#中使用TableLayoutPanel和动态Combobox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆