Alex Rivera | Logout

How to select an item in a ListView programmatically?

Asked 2011-04-26T13:41:40.330
85

I'm trying to select the first item in a ListView programmatically, but it doesn't appear to have been selected. I am using the following code:

if (listView1.Items.Count > 0)
    listView1.Items[0].Selected = true;

Actually I've had this problem before but I can't remember how I managed to solve it!

Edit
Report

1 Answer

54
if (listView1.Items.Count > 0)
{
    listView1.Items[0].Selected = true;
    listView1.Select();
}

list items do not appear selected unless the control has the focus (or you set the HideSelection property to false)

answered 2011-04-26T13:45:15.050

Your Answer