Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
507 views
in Technique[技术] by (71.8m points)

c# - How do I populate a ListView in virtual mode asynchronously?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...