0

After binding the Data grid View to a datasource I need to change the column headers on 12 columns to user friendly values and make them read only.

Grid.Columns["columnname"].HeaderText = "Column Name";
Grid.Columns["Client"].ReadOnly = true;

When the Grid contains a large dataset (10,000+ records) there is a noticeable delay in the time it takes to refresh the grid. Retrieving the data takes sub-second time. The function that changes the 12 column headertext values and setting ReadOnly = True on several others takes roughly 3-4 times that long and spikes the CPU usage to 100% for 1 core.
On Small datasets the renaming process takes negligible time, so it is clearly dependent on the size of the data set.

All column/Row Auto-sizing properties have been turned off. The DataGridView.SuspendLayout() function saves 10 milliseconds. DoubleBuffering saved 10 milliseconds.

What could the code be doing in the background that is taking so much processor power? Is it redrawing the whole grid each time those properties are changed? Is there a way to only have that redraw happen once?

DrJaul
  • 73
  • 7
  • 1
    box it with `SuspendLayout()` and `ResumeLayout()` – Ňɏssa Pøngjǣrdenlarp Feb 02 '17 at 22:26
  • why don't you alter or change the query in regards to what you want those names to be.. are you familiar with Column `Aliasing` in Sql...? – MethodMan Feb 02 '17 at 22:30
  • I tried to rename all columns of a ReadOnly DataGridView with 13,600 records and 24 columns: it's immediate. but, when I tried to do the Auto Column resizing, it took about 20 seconds. – Graffito Feb 02 '17 at 23:01
  • I tried to rename all columns of a ReadOnly DataGridView with 13,600 records and 24 columns: it's immediate. But, when I tried to do the Auto Column resizing, it took about 20 seconds. May you check that " Grid.AutoSizeColumnsMode == DataGridViewAutoSizeColumnsMode.None" ? – Graffito Feb 02 '17 at 23:10
  • @graffito-Yeah i have all the auto-size modes set to none; row and column. There was a very small time difference (-10ms on 30k records) after doing so. Still trying to pare it down from nearly 5 seconds. – DrJaul Feb 08 '17 at 14:13
  • Interested to know more about this, I wonder if there is a solution still. – Moiyd Oct 06 '21 at 15:27

1 Answers1

0

There are separate properties for row/column header autosizing. Having these set to AutoSize can also slow down redraw.

Grid.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
Grid.RowHeadersWidthSizeMode = System.Windows.Forms.DataGridViewRowHeadersWidthSizeMode.EnableResizing;
Jon McKee
  • 11
  • 5