forum

Home / DeveloperSection / Forums / C#: How to select the top item in a listview after sorting

C#: How to select the top item in a listview after sorting

Anonymous User402429-Mar-2014

I'm filling my System.Windows.Forms.ListView with results from my database as such:

foreach (DataRow row in theTable.Rows)

{

    ...build item from row..

    myListView.Items.Add(item);

}

And then I want to sort my listview in a different order than the rows come back from the DB, so I call

myListView.Sort();

But then when I want to go to select the top item in the listview it won't work, it selected something other than the top item:

myListView.Items[0].Selected = true;

Makes sense since the Items collection is added to in the order of the rows from the table iterated through in the foreach loop.

Using myListView.TopItem.Seleted = true doesn't work either.

So how do I go about selecting the topmost item in the listview AFTER I've sorted it?

Thanks for any answers.


Updated on 29-Mar-2014
I am a content writter !

Can you answer this question?


Answer

1 Answers

Liked By