如果List.Items [a] == List.SelectedItems [b],则删除项目 [英] delete item if List.Items[a]==List.SelectedItems[b]

查看:74
本文介绍了如果List.Items [a] == List.SelectedItems [b],则删除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!我不明白为什么这不能正常工作...

你能检查一下这个错误吗?

  int  a =  0 ;
 int  b =  0 ;
同时(gvList.SelectedItems.Count> 0)
{
    如果(gvList.Items [a] == gvList.SelectedItems [b])
    {
        myList.RemoveAt(a);
    }
    其他
    {
        a ++;
    }
} 



//gvList是gridview
//myList是一个列表.它是gvList

解决方案

的项目来源:尝试以下操作:

  for ( int  i =  0 ;我<  gvList.SelectedItems.Count; i ++)
{
    gvList.Items.Remove(gvList.SelectedItems [i]);
}
gvList.SelectedItems.Clear(); 



(您可能必须向后遍历SelectedItems集合)


由于gvList.SelectedItems从未更改,因此它的Count也将不会更改,因此您将永远不会退出while循环. br/>
您应该先解释一下要做什么,我们也许会找到解决方案.


确实有几个原因:

如果所选项目与您的列表不完全匹配怎么办?
"b"会怎样?
如果未选择gvList中的第一项会怎样?

您是指什么错误?


hi! i dont understand why this doesn''t work correctly...

can you please check the error with this one?

int a=0;
int b=0;
while(gvList.SelectedItems.Count>0)
{
    if(gvList.Items[a]==gvList.SelectedItems[b])
    {
        myList.RemoveAt(a);
    }
    else
    {
        a++;
    }
}



//gvList is a gridview
//myList is a list. it is the itemsource of gvList

解决方案

Try this:

for (int i = 0; i < gvList.SelectedItems.Count; i++)
{
    gvList.Items.Remove(gvList.SelectedItems[i]);
}
gvList.SelectedItems.Clear();



(you may have to traverse the SelectedItems collection backwards)


Since gvList.SelectedItems is never changed, its Count will never change, so you will never exit the while loop.

You should explain what you want to do and we will maybe find a solution.


Well several reasons really:

What happens if the selected items don''t exactly match your list?
What happens to "b"?
What happens if the first item in gvList is not selected?

Which error did you mean?


这篇关于如果List.Items [a] == List.SelectedItems [b],则删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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