Get single listView SelectedItem

topofsteel picture topofsteel · Feb 26, 2013 · Viewed 155.3k times · Source

I have the MultiSelect property of the listView set to false and I'm trying to get a single listViewItem. But the available property is SelectedItems. I've been using the following code...

foreach (ListViewItem item in listView1.SelectedItems)
{
    //do something with item.text or whatever
}

Because I know there will only be one item selected. What is the correct way of doing this?

Answer

Vogel612 picture Vogel612 · Feb 26, 2013

Usually SelectedItems returns either a collection, an array or an IQueryable.

Either way you can access items via the index as with an array:

String text = listView1.SelectedItems[0].Text; 

By the way, you can save an item you want to look at into a variable, and check its structure in the locals after setting a breakpoint.