WinForm ListView虛擬模式加載數據 提高加載速度


  將VirtualMode 屬性設置為 true 會將 ListView 置於虛擬模式。控件不再使用Collection.Add()這種方式來添加數據,取而代之的是使用RetrieveVirtualItem(Occurs when the ListView is in virtual mode and requires a ListViewItem.)和CacheVirtualItems兩個事件,單獨使用RetrieveVirtualItem也可以,CacheVirtualItems這個事件主要是為了方便編程人員操作緩沖集合,其參數CacheVirtualItemsEventArgs有StartIndex和EndIndex兩個屬性在虛擬模式下。

  在虛擬模式下,從緩沖之中獲取所需的數據進行加載,性能會有很大提高。 在其他情況下,可能需要經常重新計算 ListViewItem 對象的值,對整個集合進行此操作將產生不可接受的性能。

示例代碼:

  

 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 RetrieveVirtualItemEventHandler(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             else
33             {
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 button1_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 = "" };
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 = "";
70             listItem.SubItems.Add("哈哈");
71             myCache.Add(listItem);
72             listView1.VirtualListSize = myCache.Count;
73             listView1.Invalidate();
74         }
75 
76     }
77 
78     public class Student
79     {
80         public string Sex { get; set; }
81         public string Name { get; set; }
82     }
83 }

 

  總結

  (1)必須設置VirtualMode為true並設置VirtualListSize大小

  (2)綁定該事件RetrieveVirtualItem

  (3)如果中間更新了數據需要重新設置VirtualListSize,並調用Invalidate()方法

  (4)禁用selectedItem,在該模式下使用selectedItem將產生異常,可以用下面方法代替

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

 

裝模作樣的聲明一下:本博文章若非特殊注明皆為原創,若需轉載請保留原文鏈接(http://www.cnblogs.com/kest/p/4659421.html )及作者信息k_est

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM