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 对象的值,对整个集合进行此操作将产生不可接受的性能。

示例代码:

using System;
using System.Collections.Generic;
using System.Windows.Forms; namespace WinFormTest
{
public partial class Form1 : Form
{
private List<ListViewItem> myCache;
public Form1()
{
InitializeComponent(); myCache = new List<ListViewItem>();
} private void Form1_Load(object sender, EventArgs e)
{
listView1.View = View.Details;
listView1.VirtualMode = true; listView1.RetrieveVirtualItem += new RetrieveVirtualItemEventHandler(listView1_RetrieveVirtualItem); } void listView1_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
{
if (myCache != null )
{
e.Item = myCache[e.ItemIndex];
}
else
{
//A cache miss, so create a new ListViewItem and pass it back.
int x = e.ItemIndex * e.ItemIndex;
e.Item = new ListViewItem(x.ToString());
}
} private void button1_Click(object sender, EventArgs e)
{
List<Student> list = GetStudentList();
foreach (var item in list)
{
ListViewItem listViewItem = new ListViewItem();
listViewItem.SubItems[].Text = item.Name;
listViewItem.SubItems.Add(item.Sex);
myCache.Add(listViewItem);
}
listView1.VirtualListSize = myCache.Count;
} private List<Student> GetStudentList()
{
List<Student> list = new List<Student>();
for (int i = ; i < ; i++)
{
Student stu = new Student { Name = "student" + i, Sex = "男" };
list.Add(stu);
}
return list;
} private void button2_Click(object sender, EventArgs e)
{ ListViewItem listItem = new ListViewItem();
listItem.SubItems[].Text = "女";
listItem.SubItems.Add("哈哈");
myCache.Add(listItem);
listView1.VirtualListSize = myCache.Count;
listView1.Invalidate();
} } public class Student
{
public string Sex { get; set; }
public string Name { get; set; }
}
}

子线程+虚拟模式操作listview

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading; namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private List<ListViewItem> myCache;
private delegate void DispMSGDelegate(int index, string MSG); public Form1()
{
InitializeComponent();
this.listView1.Columns.Add("编号", , HorizontalAlignment.Right);
this.listView1.Columns.Add("姓名", , HorizontalAlignment.Right);
myCache = new List<ListViewItem>(); } private void button1_Click(object sender, EventArgs e)
{
progressBar1.Maximum = ;
Thread tWorkingThread = new Thread(Thread_DisplayMSG);
//ChaosThreadList.Add(tWorkingThread);
tWorkingThread.Start(); }
private void Thread_DisplayMSG()
{
List<Student> list = GetStudentList();
int k = ;
foreach (var item in list)
{
progressBar1.Value = k;
k++; DispMsg(k, item.Name);
}
listView1.VirtualListSize = myCache.Count;
} private void DispMsg(int iIndex, string strMsg)
{
if (this.listView1.InvokeRequired == false) //如果调用该函数的线程和控件lstMain位于同一个线程内
{
ListViewItem listViewItem = new ListViewItem();
listViewItem.SubItems[].Text = iIndex.ToString();
listViewItem.SubItems.Add(strMsg);
myCache.Add(listViewItem);
}
else //如果调用该函数的线程和控件lstMain不在同一个线程
{
//通过使用Invoke的方法,让子线程告诉窗体线程来完成相应的控件操作
DispMSGDelegate DMSGD = new DispMSGDelegate(DispMsg); //使用控件lstMain的Invoke方法执行DMSGD代理(其类型是DispMSGDelegate)
this.listView1.Invoke(DMSGD, iIndex, strMsg); }
} private List<Student> GetStudentList()
{
List<Student> list = new List<Student>();
for (int i = ; i < ; i++)
{
Student stu = new Student { Name = "student" + i, Sex = "男" };
list.Add(stu);
}
return list;
}
private void listView1_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
{
if (myCache != null)
{
e.Item = myCache[e.ItemIndex];
}
else
{
//A cache miss, so create a new ListViewItem and pass it back.
int x = e.ItemIndex * e.ItemIndex;
e.Item = new ListViewItem(x.ToString());
}
} private void Form1_Load(object sender, EventArgs e)
{
listView1.View = View.Details;
listView1.VirtualMode = true; listView1.RetrieveVirtualItem += new RetrieveVirtualItemEventHandler(listView1_RetrieveVirtualItem);
} }
public class Student
{
public string Sex { get; set; }
public string Name { get; set; }
}
}
上一篇:C++类的成员函数使用的一些小总结


下一篇:Oracle中decode函数 列变成行