John M
John M

Reputation: 14668

Refresh listview so that it shows the selectedindex?

I have a winforms listview with 200 items shown in a details listview.
50 items show at a time on the screen. I would like to hit a button and have the listview show the selected index # - for example #113.

The problem I'm having is that I can select index 113 but the listview will not show 113 at the top of the scroll range.

What do I have to do to get the listview to move to the selectindex?

UPDATE1:

The answer is to use EnsureVisible :

populateListView();
this.listView1.Items[113].Selected = true;
this.listView1.Items[113].EnsureVisible();   

Upvotes: 10

Views: 11615

Answers (3)

Devam Mehta
Devam Mehta

Reputation: 11

Add the following code for the result:

list.SelectedIndex = i;
list.ScrollIntoView(list.Items[i]);

Upvotes: 1

John
John

Reputation: 6553

Did you try using yourList.SelectedItem.EnsureVisible

Use list.TopItem = list.Item[x] to have it scroll that item to the top (or attempt to)

http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.ensurevisible%28VS.90%29.aspx

Upvotes: 16

RobotMike
RobotMike

Reputation: 311

Ensure visible will make sure the item you define is visible in the window but not necessairly the top item in the ListView.

To make sure your selected item is the top item use the listView1.TopItem property

listView1.Items[113].Selected = true;
listView1.TopItem = listView1.SelectedItems[0];

Upvotes: 4

Related Questions