How do I populate a ListView in virtual mode asynchronously?

烂漫一生 提交于 2019-12-29 07:57:12

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/336901/how-do-i-populate-a-listview-in-virtual-mode-asynchronously

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!