0

I have two comboBoxes take the data from the same List witch contains an objects. How to remove the selected item in ComboBox 1 from items in ComboBox 2?

comboBox1.DataSource = CityList;    //CityList is list contain objects
comboBox1.ValueMember = "ID";
comboBox1.DisplayMember = "Name";

comboBox2.DataSource = CityList;
comboBox2.ValueMember = "ID";
comboBox2.DisplayMember = "Name";
comboBoxTargetState.Items.Remove(comboBoxCurrentState.SelectedItem); // give me an excption
4

1 回答 1

1

使用时DataSource,您必须从源中删除项目而不是从Items(因为当 ComboBox 是数据绑定时它是只读的):

if(comboBoxCurrentState.SelectedIndex > -1)
  CityList.RemoveAt(comboBoxCurrentState.SelectedIndex);
于 2013-08-01T03:07:13.640 回答