views:

2057

answers:

2

I'd like to display records from our database in a listview - but retrieves can take a long time. I can use RetrieveVirtualItem to tell me when a new ListViewItem is needed, add a dummy item, and start a retrieve; but what do I do with the record when the database returns it? I can't update the ListView's Items collection while the ListView is in VirtualMode. Is there a way to tell the ListView to reload an item? Or can I just keep a reference to the ListViewItem and populate that? If neither of those would work, how else could I populate a ListView in virtual mode asynchronously?

+4  A: 

Your RetrieveVirtualItem handler will be called when the ListView needs updating. If your data is not available yet and you cannot wait, then you will have to create a dummy item (not handling RetrieveVirtualItem will raise an exception).

Once your data is ready, you can invalidate the control - this will call RetrieveVirtualItem again for each of the visible items. As an alternative to invalidating the whole control, you can control which items are to be redrawn by using the RedrawItems method of the ListView control, which works in virtual and regular modes:

http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.redrawitems.aspx

It sounds like it may be worth downloading your records in batches if it's due to take a while. Also, if your database operations are expensive, it's well worth investigating caching your ListItems (there's a CacheVirtualItems event you'll need to handle):

http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.cachevirtualitems.aspx

I hope this helps.

Dave R.
A: 

For details please see. ListView in Virtual Mode and you can download source there also.

bimbim.in