TransWikia.com

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

Stack Overflow Asked by nnmmss on February 22, 2021

I am two Combobox which are populated with a view of a table like following

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

when I change the value of each of them, the value of the other one changes automatically. why is that ?

One Answer

Here are two ways to avoid this issue.

Solution A:

Use BindingSource as the DataSource of 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";

Solution B:

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

Correct answer by Kyle Wang on February 22, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP