在ListView中缓慢绘制具有许多列的项目

时间:2015-06-10 11:51:17

标签: c# winforms performance listview scroll

我在虚拟模式下使用ListView。当列数增加(> 500)时 - 绘图变得太慢。滚动挂起。 请告诉我如何摆脱这个问题并提高ListView的性能?

简单的测试项目:

public partial class Form1 : Form
    {
        private const int maxLines = 1000000;//rows
        private const int columns = 1024;
        ListViewItem lvi = new ListViewItem();            

        public Form1()
        {
            InitializeComponent(); 

            //ListView Properties
            this.listView1.FullRowSelect = true;
            this.listView1.GridLines = true;
            this.listView1.MultiSelect = false;
            this.listView1.Name = "listView1";
            this.listView1.UseCompatibleStateImageBehavior = false;
            this.listView1.View = System.Windows.Forms.View.Details;
            this.listView1.RetrieveVirtualItem += new System.Windows.Forms.RetrieveVirtualItemEventHandler(this.listView1_RetrieveVirtualItem);       
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            listView1.VirtualMode = true;
            listView1.VirtualListSize = maxLines;

            PropertyInfo aProp = typeof(ListView).GetProperty("DoubleBuffered", BindingFlags.NonPublic | BindingFlags.Instance);
            aProp.SetValue(listView1, true, null);

            //create columns
            for (int i = 0; i < columns; i++)
                listView1.Columns.Add(i.ToString(), 40);

            //create test data
            for (int i = 0; i < columns; i++)
            {
                ListViewItem.ListViewSubItem lvsi = new ListViewItem.ListViewSubItem();
                lvsi.Text = i.ToString();
                lvi.SubItems.Add(lvsi);
            }
        }

        //get item in virtual mode
        private void listView1_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
        {
            e.Item = lvi;
        }

    }

1 个答案:

答案 0 :(得分:0)

您需要使用ownerdraw或customdraw仅绘制可见列。您可以使用HitTest来检查左上角的列,然后从那里开始...