Setting DataGridView Row Heights Fast

c#, datagridview, winforms

Solution

Apparently if you create the rows independently of the datagridview, the performance-hindering features do not apply.

The trick is to create an array of rows, size them, and then add the range of rows to the datagridview afterwards:

public void PopulateData()
    {
        this.SuspendLayout();

        DataGridViewRow[] rows = new DataGridViewRow[Data.RowCount];
        for (int i = 0; i < rows.Length; i++)
        {
            DataGridViewRow row = new DataGridViewRow();
            row.Height = Data.RowHeights[i];
            rows[i] = row;
        }
        this.Rows.AddRange(rows);

        this.ResumeLayout();
    }

For 15,000 rows this only took 150 ms compared to 15 seconds without creating a seperate array, 100 times faster!

Problem

I have a virtual datagridview that I want to set varying row heights for. I was hoping to find a method for setting all the row heights at once, rather than looping through each one at a time. This is the method that I tried to set the heights, but the performance is horrible ~1 second per 1,000 rows. For me, an average row count is ~20k-30k rows so this is unacceptable. ``` public void PopulateData() { this.SuspendLayout(); this.RowCount = Data.RowCount; for (int i = 0; i < Data.RowCount; i++) { this.Rows[i].Height = Data.RowHeights[i]; } this.ResumeLayout(); } ``` I made sure to turn off auto-sizing first also, but performance is still poor. ``` this.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None; this.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None; ``` Is there any way to pass in an array of row heights or prevent `OnRowHeightChanged` from being called when resizing rows?

Original source