Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

How can I get the selected item in a listview. I'm using the SelectedIndexChanged event of the listview to get the current selected index but the first time I click I always get the first item no matter which item I click, and the second time I click I get an error telling me that nothing is selected...

Any one has any idea???

Proudly a Palestinian

Microsoft ASP.NET MVP

My Blog: wessamzeidan.net

Posted

Wess,

 

The reason why you have nothing selected at some point is because this event is fired twice upon selection of another listview item.

I would suggest that you test for selection and take action only if an item is selected.

The first time around, there will be no selection, the second time should have the new item selected. Use code like this:

 

If lstListView.SelectedIndices.Count > 0 Then

'do what you need to do

'like this:

MessageBox.Show(lstListView.SelectedItems(0).Text)

'this will give you the text of the first selected item (in case

'multiple are selected

End If

 

I hope this helps...

IS
Posted

No, that is not a bug.

 

First, it is raised when the currently selected ListView Item is "un-selected". At that point, no Item is selected.

 

Next, it is raised when the new Item is selected, and that is when you want to get your code to fire...

 

I am glad it helped.

IS

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...