İbrahim Akgün
İbrahim Akgün

Reputation: 1567

Remove whole ListViewItems except first Column

I want to remove whole ListviewItems in my Listview except 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);
                }
            }

Upvotes: 0

Views: 85

Answers (1)

HCL
HCL

Reputation: 36775

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.

Upvotes: 1

Related Questions