R – Winforms Datagridview too slow with ImageColumn

datagridviewwinforms

In my winforms app, i have a datagridview that takes about 0.8 seconds to be populated with +/- 5000rows – if all columns are textbox columns.

One of the columns is an integer column, so I decided to change that column to an ImageColumn and in the Cell_formatting event of the grid, I use the following code to determine the appropriate image to display:

private void dgv_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) {
    if (dgv.Columns[e.ColumnIndex] is DataGridViewImageColumn && e.ColumnIndex == 1) {
        int cellVal = (int)e.Value;
        switch (cellVal) {
            case 1:
                e.Value = Properties.Resources.Pending;
                dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].ToolTipText = "Item pending attention";
                break;
            case 2:
                e.Value = Properties.Resources.Tick
                dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].ToolTipText = "File is available";
                break;
            default:
                break;
        }
    }
}

It works. However, the datagridview now takes almost 5 seconds to populate itself!! and sorting columns just becomes too time consuming. 5 seconds now may not seem like a lot, but the rows will grow to about 30,000 in about 2months time!!

Is there a more efficient way to handle this scenario?

Thank u!

Best Answer

If performance is a concern, you may consider using the DataGridView in virtual mode.

Related Topic