ListView BeginUpdate not working
-
I have run into a problem when updating a ListView object with hundreds or thousands of records at a time. I do a listView1.BeginUpdate before I start and an EndUpdate when I am done, but even though the contents of the listview are not updating, it still takes an inordinate amount of time to update the object (15 seconds for 2000 items). If the listview is not visible (i.e. the tab the object is on in a tab control is not the selected tab), then it updates in a 10th of the time. I know there is a LockWindowUpdate available as an API call but either I am calling it wrong or it has no effect on the listview object. Can anyone out there help me out? Darryl Borden Principal IT Analyst darryl.borden@elpaso.com
-
I have run into a problem when updating a ListView object with hundreds or thousands of records at a time. I do a listView1.BeginUpdate before I start and an EndUpdate when I am done, but even though the contents of the listview are not updating, it still takes an inordinate amount of time to update the object (15 seconds for 2000 items). If the listview is not visible (i.e. the tab the object is on in a tab control is not the selected tab), then it updates in a 10th of the time. I know there is a LockWindowUpdate available as an API call but either I am calling it wrong or it has no effect on the listview object. Can anyone out there help me out? Darryl Borden Principal IT Analyst darryl.borden@elpaso.com
This is happening because, while the control is not visible, the Windows handle is not created. When the Windows handle is not created, no sorting (and a few other things) is done. If the control is visible - even if it isn't currently painting items - it sorts with each addition to the
Items
collection! Even using theLockWindowsUpdate
API won't do anything to resolve this issue. TheBeginUpdate
andEndUpdate
methods essentially do the same thing. They disable and enable drawing (respectively) by using theWM_SETREDRAW
message. While it isn't documented,LockWindowsUpdate
probably does this very same thing. So, you'll either have to bite the bullet and accept the lag, or hide your control and reshow it when you're finished, but that might be annoying to the user. If nothing else, show a progress bar (so add the items in a separate thread, but be sure to useInvokeRequired
andInvoke
to actually add the items!) while you're adding items to the user knows that something's happening and that they should way. You should also use a try-finally block to show the wait cursor (Cursor.Current = Cursors.WaitCursor
) and, in the finally block, set it back to the default (Cursor.Current = Cursors.Default
).-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----