Slow drawing of items in ListView with many columns

563 Views Asked by At

I use a ListView in a virtual mode. When the number of columns increases (>500) - drawing becomes too slow. To scroll hangs. Please tell me how to get rid of this problem and increase the performance of ListView?

Simple test project:

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

There are 1 best solutions below

0
On

You need to use ownerdraw or customdraw to only draw visible columns. You can use HitTest to check what column is at the top left corner and start from there...