向ListView中添加大量数据的时候,往往都慢的令人难受,使用VirtualMode模式可大大改善呈现的性能.以下Demo了一下ListView使用时候的常见场景,包括加载/更新/删除/排序
namespace ListviewVirtualTest
{
partial class Form2
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
Windows 窗体设计器生成的代码
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.ColumnHeader columnHeader1;
private System.Windows.Forms.ColumnHeader columnHeader2;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
}
}
{
partial class Form2
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
Windows 窗体设计器生成的代码
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.ColumnHeader columnHeader1;
private System.Windows.Forms.ColumnHeader columnHeader2;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ListviewVirtualTest
{
public partial class Form2 : Form
{
private List<ListViewItem> m_hListViewItems;
private MySorter mySorter;
public Form2()
{
InitializeComponent();
m_hListViewItems = new List<ListViewItem>();
mySorter = new MySorter();
}
void listView_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
{
e.Item = m_hListViewItems[e.ItemIndex];
}
private void button1_Click(object sender, EventArgs e)
{
int NR = 10000;
for (int i = 0; i < NR; i++)
m_hListViewItems.Add(
new ListViewItem(new string[] { i + " test1", i + " test2" }));
listView1.RetrieveVirtualItem += new RetrieveVirtualItemEventHandler(listView_RetrieveVirtualItem);
listView1.VirtualListSize = NR;
listView1.VirtualMode = true;
}
//更新
private void button2_Click(object sender, EventArgs e)
{
for (int i = 0; i < m_hListViewItems.Count; i++)
{
m_hListViewItems[i].SubItems[0].Text = i + " " + DateTime.Now.ToString();
m_hListViewItems[i].SubItems[1].Text = i + " " + DateTime.Now.ToString();
}
listView1.Invalidate();
}
// 删除
private void button3_Click(object sender, EventArgs e)
{
List<ListViewItem> list = new List<ListViewItem>();
for (int i = 0; i < m_hListViewItems.Count; i += 2)
{
list.Add(m_hListViewItems[i]);
}
foreach (ListViewItem lvi in list)
{
m_hListViewItems.Remove(lvi);
}
listView1.VirtualListSize = m_hListViewItems.Count;
listView1.Invalidate();
}
//排序
private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
{
if (e.Column == mySorter.ColumnIndex)
{
mySorter.SortOrder = (
mySorter.SortOrder == SortOrder.Descending ?
SortOrder.Ascending : SortOrder.Descending);
}
else
{
mySorter.SortOrder = SortOrder.Ascending;
mySorter.ColumnIndex = e.Column;
}
m_hListViewItems.Sort(mySorter);
listView1.Invalidate();
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ListviewVirtualTest
{
public partial class Form2 : Form
{
private List<ListViewItem> m_hListViewItems;
private MySorter mySorter;
public Form2()
{
InitializeComponent();
m_hListViewItems = new List<ListViewItem>();
mySorter = new MySorter();
}
void listView_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
{
e.Item = m_hListViewItems[e.ItemIndex];
}
private void button1_Click(object sender, EventArgs e)
{
int NR = 10000;
for (int i = 0; i < NR; i++)
m_hListViewItems.Add(
new ListViewItem(new string[] { i + " test1", i + " test2" }));
listView1.RetrieveVirtualItem += new RetrieveVirtualItemEventHandler(listView_RetrieveVirtualItem);
listView1.VirtualListSize = NR;
listView1.VirtualMode = true;
}
//更新
private void button2_Click(object sender, EventArgs e)
{
for (int i = 0; i < m_hListViewItems.Count; i++)
{
m_hListViewItems[i].SubItems[0].Text = i + " " + DateTime.Now.ToString();
m_hListViewItems[i].SubItems[1].Text = i + " " + DateTime.Now.ToString();
}
listView1.Invalidate();
}
// 删除
private void button3_Click(object sender, EventArgs e)
{
List<ListViewItem> list = new List<ListViewItem>();
for (int i = 0; i < m_hListViewItems.Count; i += 2)
{
list.Add(m_hListViewItems[i]);
}
foreach (ListViewItem lvi in list)
{
m_hListViewItems.Remove(lvi);
}
listView1.VirtualListSize = m_hListViewItems.Count;
listView1.Invalidate();
}
//排序
private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
{
if (e.Column == mySorter.ColumnIndex)
{
mySorter.SortOrder = (
mySorter.SortOrder == SortOrder.Descending ?
SortOrder.Ascending : SortOrder.Descending);
}
else
{
mySorter.SortOrder = SortOrder.Ascending;
mySorter.ColumnIndex = e.Column;
}
m_hListViewItems.Sort(mySorter);
listView1.Invalidate();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace ListviewVirtualTest
{
public class MySorter : System.Collections.Generic.IComparer<ListViewItem>
{
public SortOrder SortOrder = SortOrder.None;
public int ColumnIndex = -1;
IComparer 成员
}
}
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace ListviewVirtualTest
{
public class MySorter : System.Collections.Generic.IComparer<ListViewItem>
{
public SortOrder SortOrder = SortOrder.None;
public int ColumnIndex = -1;
IComparer
}
}
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace ListviewVirtualTest
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form2());
}
}
}
using System.Collections.Generic;
using System.Windows.Forms;
namespace ListviewVirtualTest
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form2());
}
}
}