Listview index problem
-
hi, in my application i have a listview with populated data. when i select a row i get my results but as soon as i want to select any other row i get the following error: InvalidArgument=Value of '0' is not valid for 'index'. Parameter name: index here is my code:
private void listviewAcc_SelectedIndexChanged(object sender, EventArgs e) { _DataValues.AccountName = listviewAcc.SelectedItems[0].Text.ToString(); }
no matter what i change the index to it will give me the same error. this only works the first time i selected a record but as soon as i make a second selection i get this error :confused: pls help thanks regards -
hi, in my application i have a listview with populated data. when i select a row i get my results but as soon as i want to select any other row i get the following error: InvalidArgument=Value of '0' is not valid for 'index'. Parameter name: index here is my code:
private void listviewAcc_SelectedIndexChanged(object sender, EventArgs e) { _DataValues.AccountName = listviewAcc.SelectedItems[0].Text.ToString(); }
no matter what i change the index to it will give me the same error. this only works the first time i selected a record but as soon as i make a second selection i get this error :confused: pls help thanks regardsI had this problem too. When I debugged it, I noticed that it was calling my event handler twice if I changed from one selected item to another, but only once if I had nothing selected and I clicked one. This makes sense because when the selection changes from one item to another, it has to deselect the current item, which fires the 'Changed' event, and then it selects the new item, firing the event again. The first time the event gets called, there is no selection (SelectedItems has a size of 0), so you will get an InvalidArgument or possibly the "Object reference not set to an instance of an object" error. All you need to do is add a check to see if any items are selected. You can do that by examining the Count property of SelectedItems.
"Quality Software since 1983!"
http://www.smoothjazzy.com/ - see the "Programming" section for (freeware) JazzySiteMaps, a simple application to generate .Net and Google-style sitemaps!