Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set selected index in a listview

I have a listview on a Win Forms where I need to programmatically set the selected index. Apparently, ListView does not have a SelectedIndex property that can be set. Is there another way to do this?

like image 651
physics90 Avatar asked Dec 11 '15 17:12

physics90


1 Answers

Apparently, ListView does not have a SelectedIndex property that can be set.

Indeed and it is logical as you can have multiple Items selected.

Hence it does have the SelectedItems and also the SelectedIndices property; both are read-only.

To select an Item simply set its Selected property to true:

listView1.Items[someItemIndex].Selected = true; 

If the ListView' s MultiSelect property is false, this will deselect all other Items. If it is true the Item is added to the set of selected Items..

To de-select all Items in one go, use listView1.SelectedIndices.Clear(); or listView1.SelectedItems.Clear();..

like image 84
TaW Avatar answered Oct 16 '22 14:10

TaW