Abraham
Abraham

Reputation: 1259

InvalidArgument=Value of '0' is not valid for 'index'. Parameter name: index

I have a custom class "Person" with properties such as Name, Email, etc. I have a List called "people", and I have a listView that displays the name of each item in the "people" list.

I'm trying to remove the "people" item that the user has selected in the listView, with the following line of code in a button1.Click event:

people.RemoveAt(listView1.SelectedItems[0].Index);

But it's throwing the error in the title, however, the code right after that removes the name from the listView it works with no problem:

listView1.Items.Remove(listView1.SelectedItems[0]);

I was following the tutorial videos found here: http://www.youtube.com/watch?v=0ZoDmeAZAD8

Thank you very much.

Upvotes: 2

Views: 6815

Answers (1)

Abraham
Abraham

Reputation: 1259

Problem was I removed it from listView1 before I removed it from List, so it couldn't find what I was referring to and threw an error.

So all I had to do was switch these two (already switched)

people.RemoveAt(listView1.SelectedItems[0].Index); // removes item from people LIST.
listView1.Items.Remove(listView1.SelectedItems[0]); // removes item/person from LISTVIEW

and it works! Thank you everyone, and next time I will not post all the code in a separate link, sorry.

Upvotes: 2

Related Questions