views:

555

answers:

5

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?

+1  A: 

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
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.
GenericTypeTea
+1  A: 

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
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
A: 

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
A: 

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
A: 

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];
Wildcatter