我有一个称为combobox1和combobox2的窗体(form1)上有2个组合框。C#:使用一个数据表来填充2个不同的组合框?
每个组合框应充满存放在2个不同的表中的SQL Server 2005数据:Table 1和Table
我的意思是:combobox1 - >表1 combobox2 - >表2
我填写资料表正确的数据,然后将组合框单独绑定到它。
我的问题是:在填充2个连击后,他们两个从table2获得相同的数据。
这是我的代码:
DataTable tb1 = new DataTable();
//Filling tb1 with data got from table1
combobox1.Items.Clear();
combobox1.DataSource = tb1;
combobox1.DisplayMember = "Name";
combobox1.ValueMember = "ID";
combobox1.SelectedIndex = -1;
//filling tb1 with data got from table2
combobox2.Items.Clear();
combobox2.DataSource = tb1;
combobox2.DisplayMember = "Name";
combobox2.ValueMember = "ID";
combobox2.SelectedIndex = -1;
有什么不对?
看来,如果我得到2个不同的数据表(tb1和tb2),每件事情都会好的。
请提出任何建议。
谢谢
好吧,所以你认为我应该有2个不同的数据表填充2组合? – odiseh 2010-05-16 11:15:14
如果你有类似的数据,那么米奇的答案会更好。如果您要撤回两个完全不同的DataTable,那么只需声明两个DataTable。 – GenericTypeTea 2010-05-16 11:27:50
谢谢,但是当我们使用字符串时,为什么你上面说的是不正确的。我的意思是,我们知道,字符串是一个引用类型。 string s =“hello”; textbox1.Text = s; s =“world”; textbox2.Text = s; 在上面的代码片段中,textbox1.Text的值为“hello”,textbox2.Text的值为“world”。对? – odiseh 2010-05-16 12:09:01