收集被修改;枚举操作可能无法执行。 [英] Collection was modified; enumeration operation may not execute.

查看:127
本文介绍了收集被修改;枚举操作可能无法执行。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个列表框,有5个data.i想要从列表框中删除数据,但是当我克隆在botton上时

然后收到错误

收藏被修改;枚举操作可能无法执行。



我的编码是

Hi,

I have a list box there is 5 data.i want to remove data from listbox but when i am clik on botton
then getting error
Collection was modified; enumeration operation may not execute.

my Coding is

foreach (ListItem selectItem in lstSelectPages.Items)
{
    if (selectItem.Selected == true)
    {
        lstSelectPages.Items.Remove(lstSelectPages.SelectedItem);
        lstSelectPages.DataBind();      
    }
}



请帮帮我


please help me out

推荐答案

循环不是需要的是:

The loop is not needed at all:
if (lstSelectPages.SelectedItem != null)
{
    lstSelectPages.Items.Remove(lstSelectPages.SelectedItem);
    lstSelectPages.DataBind();
}


Foreach不允许集合的manupulation。尝试循环,如图所示。





Foreach does not allow the manupulation of the collection. Try for loop as shown.


for(int x=0; x < lstSelectPages.Items.Count;x++)
            {
                if (lstSelectPages.SelectedItem == lstSelectPages.Items[x])
                {
                    lstSelectPages.Items.RemoveAt(x);
                    lstSelectPages.DataBind();
                }
            }


这篇关于收集被修改;枚举操作可能无法执行。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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