ListView的虚拟模式

转载:http://www.cnblogs.com/hcfalan/articles/1238493.html
向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());

        }

    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值