WinForm ListView increases the loading speed by loading data in virtual mode.

Source: Internet
Author: User

WinForm ListView increases the loading speed by loading data in virtual mode.

Setting the VirtualMode attribute to true places the ListView in the virtual mode. The control no longer uses Collection. add () in this way to Add data, instead of using RetrieveVirtualItem (Occurs when the ListView is in virtual mode and requires a ListViewItem .) the CacheVirtualItems and CacheVirtualItems events can also be used independently. The CacheVirtualItems event is mainly used to facilitate the programming staff to buffer sets. The CacheVirtualItemsEventArgs parameter has the StartIndex and EndIndex attributes in virtual mode.

In virtual mode, you can obtain the required data from the buffer for loading, which greatly improves the performance. In other cases, you may need to re-calculate the value of the ListViewItem object frequently. This operation on the entire set will produce unacceptable performance.

Sample Code:

  

1 using System; 2 using System. collections. generic; 3 using System. windows. forms; 4 5 namespace WinFormTest 6 {7 public partial class Form1: Form 8 {9 private List <ListViewItem> myCache; 10 public Form1 () 11 {12 InitializeComponent (); 13 14 myCache = new List <ListViewItem> (); 15} 16 17 private void Form1_Load (object sender, EventArgs e) 18 {19 listView1.View = View. details; 20 listView1.VirtualMode = True; 21 22 listView1.RetrieveVirtualItem + = new feature (listView1_RetrieveVirtualItem); 23 24} 25 26 void listView1_RetrieveVirtualItem (object sender, RetrieveVirtualItemEventArgs e) 27 {28 if (myCache! = Null) 29 {30 e. item = myCache [e. itemIndex]; 31} 32 else33 {34 // A cache miss, so create a new ListViewItem and pass it back.35 int x = e. itemIndex * e. itemIndex; 36 e. item = new ListViewItem (x. toString (); 37} 38} 39 40 private void button#click (object sender, EventArgs e) 41 {42 List <Student> list = GetStudentList (); 43 foreach (var item in list) 44 {45 ListViewItem listViewItem = new ListViewItem (); 46 listViewItem. subItems [0]. text = item. name; 47 listViewItem. subItems. add (item. sex); 48 myCache. add (listViewItem); 49} 50 listView1.VirtualListSize = myCache. count; 51} 52 53 private List <Student> GetStudentList () 54 {55 List <Student> list = new List <Student> (); 56 for (int I = 0; I <2000; I ++) 57 {58 Student stu = new Student {Name = "student" + I, Sex = "male"}; 59 list. add (stu); 60} 61 return list; 62} 63 64 65 private void button2_Click (object sender, EventArgs e) 66 {67 68 ListViewItem listItem = new ListViewItem (); 69 listItem. subItems [0]. text = "female"; 70 listItem. subItems. add ("Haha"); 71 myCache. add (listItem); 72 listView1.VirtualListSize = myCache. count; 73 listView1.Invalidate (); 74} 75 76} 77 78 public class Student79 {80 public string Sex {get; set;} 81 public string Name {get; set ;} 82} 83}

 

Summary

(1) VirtualMode must be set to true and VirtualListSize must be set.

(2) bind the event RetrieveVirtualItem

(3) If data is updated in the middle, you need to reset VirtualListSize and call the Invalidate () method.

(4) Disable selectedItem. Using selectedItem in this mode will generate an exception. You can use the following method instead.

private List<ListViewItem> FindSelectedAll(){    List<ListViewItem> r = new List<ListViewItem>();    foreach (int item in listView1.SelectedIndices)    {         r.Add(bufferItems[item]);     }     return r; } 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.