组合框数据绑定(独立选择)

时间:2015-05-17 18:15:36

标签: c# winforms combobox datasource

我在两个不同的组合框中绑定一些数据,如下所示:

BindingList<Customer> customers = new BindingList<Customer>();
customer.Add(new Customer(1, "Mike"));
customer.Add(new Customer(2, "Max"));
customer.Add(new Customer(3, "Taylor"));

combobox1.DisplayMember = "Name";
combobox1.ValueMember = "Id";
combobox1.DataSource = customers;

combobox2.DisplayMember = "Name";
combobox2.ValueMember = "Id";
combobox2.DataSource = customers;

它运行良好,但是如果我在combobox1中选择了一个项目,这个项目会自动选择在combobox2中。如何解决这个问题(需要独立选择)?

1 个答案:

答案 0 :(得分:1)

以下代码 为我无缝工作:

BindingList<Customer> customer = new BindingList<Customer>();
customer.Add(new Customer(1, "Mike"));
customer.Add(new Customer(2, "Max"));
customer.Add(new Customer(3, "Taylor"));

BindingList<Customer> customer2 = new BindingList<Customer>(customer);

combobox1.DisplayMember = "Name";
combobox1.ValueMember = "Id";
combobox1.DataSource = customer;

combobox2.DisplayMember = "Name";
combobox2.ValueMember = "Id";
combobox2.DataSource = customer2;