向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 窗體設計器生成的代碼#region Windows 窗體設計器生成的代碼
/**//// <summary>
/// 設計器支持所需的方法 - 不要
/// 使用代碼編輯器修改此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.listView1 = new System.Windows.Forms.ListView();
this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(1, 2);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// listView1
//
this.listView1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.columnHeader1,
this.columnHeader2});
this.listView1.Location = new System.Drawing.Point(1, 31);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(592, 388);
this.listView1.TabIndex = 1;
this.listView1.UseCompatibleStateImageBehavior = false;
this.listView1.View = System.Windows.Forms.View.Details;
this.listView1.ColumnClick += new System.Windows.Forms.ColumnClickEventHandler(this.listView1_ColumnClick);
//
// columnHeader1
//
this.columnHeader1.Width = 138;
//
// columnHeader2
//
this.columnHeader2.Width = 214;
//
// button2
//
this.button2.Location = new System.Drawing.Point(82, 2);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 2;
this.button2.Text = "button2";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(163, 2);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 23);
this.button3.TabIndex = 3;
this.button3.Text = "button3";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(595, 431);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.listView1);
this.Controls.Add(this.button1);
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false);
}
#endregion
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;
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
成員#region IComparer<ListViewItem> 成員
public int Compare(ListViewItem x, ListViewItem y)
{
ListViewItem hLeft = x;
ListViewItem hRiht = y;
if (SortOrder == SortOrder.Ascending && (ColumnIndex >= 0) && (ColumnIndex < hLeft.SubItems.Count))
{
string sx = hLeft.SubItems[ColumnIndex].Text;
string sy = hRiht.SubItems[ColumnIndex].Text;
return sx.CompareTo(sy);
}
if (SortOrder == SortOrder.Descending && (ColumnIndex >= 0) && (ColumnIndex < hLeft.SubItems.Count))
{
string sx = hLeft.SubItems[ColumnIndex].Text;
string sy = hRiht.SubItems[ColumnIndex].Text;
return sy.CompareTo(sx);
}
return 0;
}
#endregion
}
}
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());
}
}
}