Improve performance on DataGridView if using it in Virtual Mode

744 Views Asked by At

Setting RowCount on DataGridView in Virtual Mode is really slow.

When using a data grid view in Virtual Mode, you have to manually specify the RowCount. This is sometimes really slow, especially when dealing with a huge volume of data. Performance is going to be amazingly slow if you need to decrease the row count (in my case, setting the row count was taking longer than 20 seconds).

2

There are 2 best solutions below

0
On

The way to improve this is to clear the rows on the data grid view before you set the row count (possibly on the ListChange event on the binding source).

datagridview.Rows.Clear()
datagridview.RowCount = count

By doing so, this will improve the performance enormously (from over 20 seconds, down to less than a second).

Speeding up setting of DataGridView.RowCount

implementing virtual mode for a datagridview that is databound

1
On

Sometimes the reason that setting RowCount takes a long time is that there are columns whose AutoSizeMode is set to something other than None. If you need other settings, you could temporarily set them to none, then set RowCount, then set them to what you really wanted.