3

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

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

单击按钮时,我想从中删除一个项目。

ddlMobileNumbers.Items.RemoveAt(i);

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

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

ddlNumbers.DataSource = myNewList

但不起作用。

我在这里做错了什么?

4

1 回答 1

5

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.

Something like,

//Null the datasource
Combobox1.Datasource = null;

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

//Set the source again
Combobox1.Datasource = ddlMobileNumbers;
于 2013-09-30T05:27:37.687 回答