简体   繁体   English

从ComboBox 2中删除从ComboBox 1中选择的项目

[英]Remove the item from ComboBox 2 that was selected from ComboBox 1

I have two comboBoxes take the data from the same List witch contains an objects. 我有两个comboBoxes从相同的List中获取数据,其中包含一个对象。 How to remove the selected item in ComboBox 1 from items in ComboBox 2? 如何从ComboBox 2中的项目中删除ComboBox 1中的选定项目?

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

When using DataSource , you have to remove the item from source instead from Items (because it's readonly when the ComboBox is data bound): 使用DataSource ,必须从源中删除项目,而不是从Items删除Items (因为当ComboBox绑定数据时,它是只读的):

if(comboBoxCurrentState.SelectedIndex > -1)
  CityList.RemoveAt(comboBoxCurrentState.SelectedIndex);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM