从 ComboBox (DropDownList) Winforms 中删除项目 [英] Remove item from ComboBox (DropDownList) Winforms

查看:19
本文介绍了从 ComboBox (DropDownList) Winforms 中删除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个已填充的下拉列表

I have a dropdownlist which has been populated

ddlNumbers.DisplayMember = "PhoneNumber";
ddlNumbers.DataSource = mobileList;
ddlNumbers.SelectedItem = null;

当一个按钮被点击时,我想从中删除一个项目.

When a button is clicked I want to remove an item from it.

ddlMobileNumbers.Items.RemoveAt(i);

但是得到例外.'设置 DataSource 属性后无法修改项目集合...'

我还尝试将集合重新分配给 DataSource

I have also tried re-assigning a collection to the DataSource

ddlNumbers.DataSource = myNewList

但不起作用.

我在这里做错了什么?

推荐答案

当列表项绑定到控件时,您不能从列表中删除它,您可以暂时null该列表的数据源绑定控件并从列表中删除项目,然后重新设置数据源.

You can't remove an item from a list when its bound to a control, You can temporarily null the data source of the bound control and remove the item from list and then set the data source again.

类似的东西,

//Null the datasource
Combobox1.Datasource = null;

//Remove the item
ddlMobileNumbers.Items.RemoveAt(i);

//Set the source again
Combobox1.Datasource = ddlMobileNumbers;

这篇关于从 ComboBox (DropDownList) Winforms 中删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆