WinForms ListView, Remembering Scrolled Location on Reload

11,553

Solution 1

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).

Solution 2

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.

Solution 3

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)
{ }

Solution 4

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.

Solution 5

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.

Share:
11,553

Related videos on Youtube

djdd87
Author by

djdd87

Updated on December 27, 2020

Comments

  • djdd87
    djdd87 over 3 years

    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?

  • djdd87
    djdd87 about 15 years
    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.
  • TheAgent
    TheAgent about 14 years
    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?
  • RichieHindle
    RichieHindle about 7 years
    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? :-)
  • fadden
    fadden almost 6 years
    FWIW, I don't see TopItemIndex in the WinForms property list: msdn.microsoft.com/en-us/library/…
  • Koray
    Koray about 5 years
    It won't work with LargeIcon, throwing: "Cannot get the top item in LargeIcon, SmallIcon, or Tile view." How nice..
  • Shino Lex
    Shino Lex about 5 years
    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?
  • Homer1982
    Homer1982 about 2 years
    I use this technique too. however, the drawback is that removing items is really slow, so I'm trying this stackoverflow.com/a/16862350/4218160, but then I fall into the problem of this question.

Related