更改一个组合框的值会导致另一个组合框的值发生变化

changing the value of one combobox causes the value of the other one changes

我是两个组合框,其中填充了一个 table 的视图,如下所示

  recieverComboBox.DataSource = myDataSet.Tables[0].DefaultView; 
  recieverComboBox.DisplayMember = "UserPosition";
  recieverComboBox.ValueMember = "ID";

  recieverUnReadComboBox.DataSource = myDataSet.Tables[0].DefaultView;
  recieverUnReadComboBox.DisplayMember = "UserPosition";
  recieverUnReadComboBox.ValueMember = "usr_Id";

当我更改其中每一个的值时,另一个的值会自动更改。这是为什么?

这里有两种方法可以避免这个问题。

解A:

使用 BindingSource 作为 ComboBox 的数据源。

recieverComboBox.DataSource = new BindingSource(myDataSet.Tables[0].DefaultView, null);
recieverComboBox.DisplayMember = "UserPosition";
recieverComboBox.ValueMember = "ID";

recieverUnReadComboBox.DataSource = new BindingSource(myDataSet.Tables[0].DefaultView, null);
recieverUnReadComboBox.DisplayMember = "UserPosition";
recieverUnReadComboBox.ValueMember = "usr_Id";

方案B:

调用方式DataTable.Copy

recieverComboBox.DataSource = myDataSet.Tables[0].Copy();
recieverComboBox.DisplayMember = "UserPosition";
recieverComboBox.ValueMember = "ID";

recieverUnReadComboBox.DataSource = myDataSet.Tables[0].Copy();
recieverUnReadComboBox.DisplayMember = "UserPosition";
recieverUnReadComboBox.ValueMember = "usr_Id";