改变一个组合框的值会导致另一个组合框的值改变

Posted

技术标签:

【中文标题】改变一个组合框的值会导致另一个组合框的值改变【英文标题】:changing the value of one combobox causes the value of the other one changes 【发布时间】:2021-01-20 05:12:32 【问题描述】:

我是两个组合框,其中填充了如下表的视图

  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";

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

【问题讨论】:

你有为 selectedindexchanged 事件编写的事件处理程序吗? @ChetanRanpariya:是的,我有 【参考方案1】:

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

方案一:

使用 BindingSource 作为 ComboBox 的 DataSource。

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";

【讨论】:

以上是关于改变一个组合框的值会导致另一个组合框的值改变的主要内容,如果未能解决你的问题,请参考以下文章

尝试在 mousepress 事件中使用来自另一个组合框的值加载自定义组合框

如何使用组合框根据另一个组合框的值从不同的表中选择数据

在 ExtJS 中获取组合框的值

extjs:加载时如何设置组合框的值

如何将组合框的值转换为 vba 模块中的字符串?

如何设置具有两列的组合框的值?