开发者

Remove whole ListViewItems except first Column

I want to remove whole ListviewItems in my Listview e开发者_运维知识库xcept first Column. I have got a method but it sometimes throw ArgumentRangeException that i could not find why.

  private void ListViewClear()
    {

            for (int i = 0; i < lstKullanicilar.Items.Count; i++)
            {
                if (lstKullanicilar.Items[i].SubItems.Count != 1)
                {
                    lstKullanicilar.Items[i].SubItems.RemoveAt(1);
                    lstKullanicilar.Items[i].SubItems.RemoveAt(2);
                    lstKullanicilar.Items[i].SubItems.RemoveAt(3);
                    lstKullanicilar.Items[i].SubItems.RemoveAt(1);
                    lstKullanicilar.Items[i].SubItems.RemoveAt(1);
                }
            }


Try somethin like this:

 for (int i = 0; i < lstKullanicilar.Items.Count; i++) {
   while(lstKullanicilar.Items[i].Count > 1){
      lstKullanicilar.Items[i].SubItems.RemoveAt(1);
   }
 }

The problem with your code is probably that you have a variable amount of items in the SubItems-collection. WIth the code you showed, you must at least have 6 items in the subitems-collection, for not getting an arugment exception.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜