使用单个数据表为组合框分配不同的绑定

时间:2015-04-18 11:00:23

标签: c# data-binding combobox

我使用7个组合框和文本框来显示某些项目 我有DataTable中的数据。所以我将数据表绑定到组合框和文本框,如

cmbIngredient1.DataSource = dt;
cmbIngredient1.DisplayMember = "Ingredient";
cmbIngredient1.ValueMember = "Ingredient";
txtTotalIngre1.DataBindings.Clear();
txtTotalIngre1.DataBindings.Add("Text", dt, "No Of Pieces");

cmbIngredient2.DataSource = dt;
cmbIngredient2.DisplayMember = "Ingredient";
cmbIngredient2.ValueMember = "Ingredient";
txtTotalIngre2.DataBindings.Clear();
txtTotalIngre2.DataBindings.Add("Text", dt, "No Of Pieces");

//Similar code for all 7 combobox and textboxes.//

正在显示数据。但是当我改变其中的combobox1其他组合框项目也在改变..

我想要这样:当更改了combobox1项目时,只应更改textbox1 当comboxbox2项目更改时,textbox2应该更改。

我是否需要创建单独的数据表并绑定以实现此目的?

1 个答案:

答案 0 :(得分:0)

我通过将数据表复制到另一个Datatable

实例来解决这个问题
DataTable dt2 = dt;  
cmbIngredient2.DataSource = dt2;
cmbIngredient2.DisplayMember = "Ingredient";
cmbIngredient2.ValueMember = "Ingredient";
txtTotalIngre2.DataBindings.Clear();
txtTotalIngre2.DataBindings.Add("Text", dt2, "No Of Pieces");