【发布时间】:2016-08-17 11:01:03
【问题描述】:
我有一个与 resx 文件的下拉绑定。当我要删除一些值时,在 foreach 的第一个循环之后我收到错误:
集合已修改;枚举操作可能无法执行
代码
foreach (ListItem item in VoucherTypeDropDownList.Items)
{
if (!availableVoucherTypesArray.Contains(int.Parse(item.Value)))
{
VoucherTypeDropDownList.Items.Remove(
VoucherTypeDropDownList.Items.FindByValue(item.Value.ToString()));
}
}
请问如何解决这个问题?
==>我已经这样解决了
for (Int32 i = VoucherTypeDropDownList.Items.Count-1; i >= 0; i--)
{
ListItem item = VoucherTypeDropDownList.Items[i];
if (!availableVoucherTypesArray.Contains(int.Parse(item.Value)))
{
VoucherTypeDropDownList.Items.Remove(VoucherTypeDropDownList.Items.FindByValue(item.Value.ToString()));
}
}
现在它工作正常。谢谢!
【问题讨论】: