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

查看:53
本文介绍了从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

但不起作用。

我在这里做什么错了? p>

What am I doing wrong here?

推荐答案

当项目绑定到控件时,您不能从列表中删除该项目,可以暂时 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天全站免登陆