4

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?

ChandlerPelhams
  • 1,648
  • 4
  • 38
  • 56

2 Answers2

3

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!

ChandlerPelhams
  • 1,648
  • 4
  • 38
  • 56
1

Try this instead of your code & see if you have any performance gains. Usually with virtual grids this works faster -

Add a handler for DataGridView.RowPrePaint:

dataGridView1.RowPrePaint += new DataGridViewRowPrePaintEventHandler(dataGridView1_RowPrePaint);

private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
{
    dataGridView1.AutoResizeRow(e.RowIndex);
}
Angshuman Agarwal
  • 4,796
  • 7
  • 41
  • 89