14

I've got a list view that I'm populating with 8 columns of user data. The user has the option to enable auto refreshing, which causes the ListView to be cleared and repopulated with the latest data from the database.

The problem is that when the items are cleared and repopulated, the visible area jumps back to the top of the list. So if I'm looking at item 1000 of 2000, it's very inconvenient to get back to that item.

Basically, what I'm asking is, how do I get the current scroll distances (x and y) and then restore them?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
djdd87
  • 67,346
  • 27
  • 156
  • 195

9 Answers9

15

I just wanted to provide some information for those who desperately try to use the buggy ListView.TopItem property:

  1. You MUST set the TopItem property AFTER calling ListView.EndUpdate
  2. The items of the ListView control MUST have their Text property set to something other than String.Empty, or the property won't work.
  3. Setting the ListView.TopItem throws null reference exceptions intermittently. Always keep this line of code inside a Try...Catch block.

Of course, this will cause the ListView's scrollbar to jump to 0 and back to the location of the top item, which is annoying. Please update this question if you find a workaround to this problem.

TheAgent
  • 1,472
  • 5
  • 22
  • 42
  • I've found that I get an off-by-one error when assigning to TopItem (that is, it scrolls to the item above the one I ask for) but if I call it twice then it works. Buggy, you say? :-) – RichieHindle Feb 24 '17 at 00:05
  • It certainly work only when I put it after EndUpdate but it's not throwing any errors without try-catch? Did it fixed by now or should I put try catch anyway? – Shino Lex Mar 15 '19 at 06:45
  • looks like it doesn't work when you have groups in details view – Alex P. Apr 30 '22 at 12:10
9

I used the following successfully:

int topItemIndex = 0;
try
{
     topItemIndex = listView1.TopItem.Index;
}
catch (Exception ex)
{ }
listView1.BeginUpdate();
listView1.Items.Clear();
//CODE TO FILL LISTVIEW GOES HERE
listView1.EndUpdate();
try 
{ 
    listView1.TopItem = listView1.Items[topItemIndex];
}
catch (Exception ex)
{ }
Dave Lucre
  • 1,105
  • 1
  • 14
  • 16
  • It won't work with LargeIcon, throwing: "Cannot get the top item in LargeIcon, SmallIcon, or Tile view." How nice.. – Koray Feb 25 '19 at 16:22
6

I had the same problem with a while ago and I ended up implementing an algorithm to compare the model with the list, so I only added/removed elements that had changed. This way if there were no massive changes the list didn't jump to the beginning. And the main thing I wanted to achieve was the efficiency (so that the list doesn't blink).

Grzenio
  • 35,875
  • 47
  • 158
  • 240
  • This is what I did. I added a tag to the first sub item and then used that to do a comparison and only update when required. – djdd87 Mar 09 '09 at 15:14
  • I use this technique too. however, the drawback is that removing items is really slow, so I'm trying this https://stackoverflow.com/a/16862350/4218160, but then I fall into the problem of this question. – Homer1982 Mar 04 '22 at 14:54
3

The TopItemIndex property on ListView is what you are looking for, however it has some confirmed bugs that should have been addressed in VS2010 release.. not sure (haven't checked).

Anyway, my workaround for making this work is to do this:

listViewOutput.TopItemIndex = outputList.Count - 1;
listViewOutput.TopItemIndex = myNewTopItemIndex;

For some reason setting it directly does not update it, but setting it to the last item and then the one I want works reliably for me.

  • FWIW, I don't see TopItemIndex in the WinForms property list: https://msdn.microsoft.com/en-us/library/system.windows.forms.listview_properties(v=vs.110).aspx – fadden Jul 08 '18 at 21:23
2

Look at the ListView.TopItem property. It has an index, which should contain its position in the list. Find that index in the new list, and set TopItem to that item, and it should do the scrolling automatically.

Chris Doggett
  • 19,959
  • 4
  • 61
  • 86
  • The ListView.TopItem doesn't seem to work. I thought it might have something to do with sorting so I disabled it, but when I set the TopItem to an item and check the property immediately after that it doesn't get changed (changes to another item, not the one I specified). Do you have any idea? – TheAgent Jan 24 '10 at 11:42
1

Unfortunately you will need to use some interop to scroll to the exact position in the ListView. Use GetScrollInfo winapi function to get the existing scroll position and SendMessage to scroll to the position.

There in an article on CodeProject named Scrolling to a group with a ListView that might guide you to the solution.

Aleris
  • 7,981
  • 3
  • 36
  • 42
0

In my tests, you did not even need the TopItem, although I used a int to save the selected item. Also TopItem throws an exception if you are using View.Tile or View.LargeIcon.

This code does not move the scroll bars:

listView1.BeginUpdate();
listView1.Items.Clear();

// loop through your add routine
listView1.Items.Add(lvi);

listView1.EndUpdate();
Conrad de Wet
  • 477
  • 6
  • 15
0

I was having sort-of the same problem. I have a listView that I populate every 1/2 sec and when I set the TopItem to an ListItem whose index > visible items, then the list jumped between the topItem and back 2 spots.

So, to correct the problem, I set the TopIterm AFTER the call to EndUpdate.

lvB.EndUpdate();
lvI.EndUpdate();
lvR.EndUpdate();

if (lstEntryInts.Items.Count > 0)
    lstEntryInts.TopItem = lstEntryInts.Items[iTopVisIdx];
if (lstEntryBools.Items.Count > 0)
    lstEntryBools.TopItem = lstEntryBools.Items[iTopVisIdx];
if (lstEntryReals.Items.Count > 0)
    lstEntryReals.TopItem = lstEntryReals.Items[iTopVisIdx];​
jeremy
  • 9,965
  • 4
  • 39
  • 59
0

My solution to maintaining scroll position:

Form level variable:

private static int scrollSpot = 0;

Inside listview refresh (ie Timer,button) to store the current spot:

scrollSpot = this.listView1.TopItem.Index;
refreshTheForm();

Inside refreshTheForm method to show the stored spot (put at very end of method):

if (scrollSpot <= 1)
{
     listView1.Items[scrollSpot].Selected = true;
}
else
{
     listView1.Items[scrollSpot - 2].Selected = true;
}
listView1.TopItem = listView1.SelectedItems[0]; 
John M
  • 14,338
  • 29
  • 91
  • 143