ListView Control

  • Thread starter Thread starter Scott Toney
  • Start date Start date
S

Scott Toney

I have a list view that I am populating from a database. I am doing some
manipulation on the data and then I want the listview to "refresh", so I am
trying to remove the items / subitems in the list box with:
If (lvChief.Items.Count > 0) Then
For Index = 1 To lvChief.Items.Count

lvChief.Items.Remove(lvChief.Items.Item(0))

Next

End If

I created a button to do lvChief.Items.Remove(lvChief.Items.Item(0)), the
number of times that corresponds to the number of entries in my listview, it
removes them, but when I use the above steps, I get an:

System.ArgumentOutOfRangeException:index

Thanks

Scott
 
1. Iterating a collection and removing from it at the same time is not
generally advisable

2. When iterating the listview (or any other type from the Collections
namespace), change the "Index = 1" to start at 0 instead

3. Why not use the Items.Clear method?

Cheers
Daniel
 
I appreciate all of the suggestions, I changed to lvChief.Items.clear and I
get a:
System.ArgumentOutOfRangeException:index
 
Are you handling the SelectedIndexChanged event (or any others) from the
listview? If yes, show us the code (you should be checking in there that
there actually selected items before accessing any items).

Cheers
Daniel
 
Thanks a bunch, that is it.

Daniel Moth said:
Are you handling the SelectedIndexChanged event (or any others) from the
listview? If yes, show us the code (you should be checking in there that
there actually selected items before accessing any items).

Cheers
Daniel
 
Back
Top