UncleRonin
Well-known member
I'm sure by now most of us have come across and/or used a DGV in some way or another. Personally I only ever used it for small result sets until recently and never had any issues with it. Now, however, I have had to use it for thousands of results (maxed at 50000 only) and even then there is a very noticeable performance hit in a lot of ways. I'm sure there must be info on these somewhere amongst the existing threads... but I can't find it!
I'm a huge fan of inhouse or original .NET Framework tools and avoid using anyone else's libraries and prefer to use the tools I have on hand rather than buy or even just use someone else's. This is not because I'm cheap (even though I am!) but because the less dependencies I have on external tools and the more control I have over my projects the better. I'm lazy!
What I would like to know is how you get around your DGV problems and what is the best way for handling the various DGV issues?
From my own experience and after reading some of Cjard's posts (they're in here somewhere!) here are some basics...
Filling the DGV takes too long
Don't ever manually add data to a DGV by adding new rows and columns unless there is only a small amount of it - by small I mean less than a thousand rows... actually... make that 100.
Always use a DataSet, DataTable, etc. as the DataSource if you need a local copy of your data.
Bind your data whenever you can because it is much faster than the alternatives.
Updating the display takes too long and flickers: during loading
When loading your DGV with data suspend your layout by calling .SuspendLayout() to prevent any updates to the display while the data is loaded. Once your data is loaded resume your layout by calling .ResumeLayout() and everything will update once only. Someone correct me if I'm wrong but every time the data of a DGV updates, the DGV updates it's display and if you add 1000 rows of data this means the display is updated 1000 times. Rather first suspend the layout, add the data and then resume the layout so this only happens once.
Updating the display takes too long and flickers: after loading
This can be handled by changing the DGV's display mechanism either through VirtualMode or by creating your own custom DGV control as an extension of the existing DGV class. I have never used VirtualMode though so I can't give any advice there. As for extending the DGV, you can try and enable DoubleBuffering which will remove the flicker but it can look laggy during scrolling and things like that and it does mess up the effects for column swapping and column and row resizing. I'm sure with some effort this can be sorted out though.
AutoSizing the columns takes too long and slows down the DGV
If you want your columns to automatically size themselves just remember that the DGV has to process every single cell of data in a column before it can find the correct width and the greater the number of cells the longer this will take.
The best way to handle this is to autosize only during loading/updating and then to disable it otherwise. Autosizing happens pretty much anytime you do something in a DGV, even when sorting a column (where the data doesn't actually change and the widths remain the same), so use it only when it's needed.
I'm a huge fan of inhouse or original .NET Framework tools and avoid using anyone else's libraries and prefer to use the tools I have on hand rather than buy or even just use someone else's. This is not because I'm cheap (even though I am!) but because the less dependencies I have on external tools and the more control I have over my projects the better. I'm lazy!
What I would like to know is how you get around your DGV problems and what is the best way for handling the various DGV issues?
From my own experience and after reading some of Cjard's posts (they're in here somewhere!) here are some basics...
Filling the DGV takes too long
Don't ever manually add data to a DGV by adding new rows and columns unless there is only a small amount of it - by small I mean less than a thousand rows... actually... make that 100.
Always use a DataSet, DataTable, etc. as the DataSource if you need a local copy of your data.
Bind your data whenever you can because it is much faster than the alternatives.
Updating the display takes too long and flickers: during loading
When loading your DGV with data suspend your layout by calling .SuspendLayout() to prevent any updates to the display while the data is loaded. Once your data is loaded resume your layout by calling .ResumeLayout() and everything will update once only. Someone correct me if I'm wrong but every time the data of a DGV updates, the DGV updates it's display and if you add 1000 rows of data this means the display is updated 1000 times. Rather first suspend the layout, add the data and then resume the layout so this only happens once.
Updating the display takes too long and flickers: after loading
This can be handled by changing the DGV's display mechanism either through VirtualMode or by creating your own custom DGV control as an extension of the existing DGV class. I have never used VirtualMode though so I can't give any advice there. As for extending the DGV, you can try and enable DoubleBuffering which will remove the flicker but it can look laggy during scrolling and things like that and it does mess up the effects for column swapping and column and row resizing. I'm sure with some effort this can be sorted out though.
AutoSizing the columns takes too long and slows down the DGV
If you want your columns to automatically size themselves just remember that the DGV has to process every single cell of data in a column before it can find the correct width and the greater the number of cells the longer this will take.
The best way to handle this is to autosize only during loading/updating and then to disable it otherwise. Autosizing happens pretty much anytime you do something in a DGV, even when sorting a column (where the data doesn't actually change and the widths remain the same), so use it only when it's needed.
VB.NET:
Dim Width As Integer = 0
For Each Column As DataGridViewColumn In Grid.Columns
Column.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
Width = Column.Width
Column.AutoSizeMode = DataGridViewAutoSizeColumnMode.None
Column.Width = Width
Next