---
title: "C#: How to select the top item in a listview after sorting"  
description: "C#: How to select the top item in a listview after sorting"  
author: "Anonymous User"  
published: 2014-03-29  
updated: 2014-03-29  
canonical: https://www.mindstick.com/forum/2004/c-sharp-how-to-select-the-top-item-in-a-listview-after-sorting  
category: "asp.net mvc"  
tags: ["asp.net mvc"]  
reading_time: 1 minute  

---

# C#: How to select the top item in a listview after sorting

I'm [filling](https://yourviews.mindstick.com/audio/1303/the-power-of-silence-what-happens-when-you-stop-filling-every-moment) my System.Windows.Forms.[ListView](https://www.mindstick.com/articles/12744/listview-in-android) with [results](https://yourviews.mindstick.com/story/4497/usage-of-baking-soda-magical-results) from my [database](https://www.mindstick.com/articles/12226/use-of-database-in-sencha-extjs-and-insert-record-from-user-form-using-ajax) 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](https://www.mindstick.com/articles/12276/how-timely-order-deliveries-can-improve-customer-experience) than the rows come back from the DB, so I call

myListView.Sort();

But then when I want to go to [select](https://www.mindstick.com/forum/160534/orderby-then-select-vs-select-then-orderby-performance) 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](https://www.mindstick.com/articles/1718/collections-in-java) is added to in the order of the rows from the [table](https://www.mindstick.com/articles/43918/how-to-design-table-using-bootstrap) iterated through in the [foreach loop](https://www.mindstick.com/forum/182/problem-in-getting-the-value-from-array-by-using-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.

## Replies

### Reply by Pravesh Singh

Hi Ben,

```
private void Populate(object sender, EventArgs e)    {          
listView1.Items.Add("D");       
listView1.Items.Add("B");       
listView1.Items.Add("A");       
listView1.Items.Add("C");    }    private void SelectFirst(object sender, EventArgs e)    {       
listView1.Items[0].Selected = true;       
listView1.Select();    }    private void SortAndSelect(object sender, EventArgs e)    {       
listView1.Sorting = SortOrder.Ascending;       
listView1.Sort();       
listView1.Items[0].Selected = true;       
listView1.Select();    }
```


---

Original Source: https://www.mindstick.com/forum/2004/c-sharp-how-to-select-the-top-item-in-a-listview-after-sorting

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
