WPF DataGrid is very slow to render
Solution 1:
Are you have:
- Enabled
VirtualizingStackPanel.VirtualizationMode
for a Grid? if not - try to set. - Set VirtualizingStackPanel.IsVirtualizing="true" for DataGrid
- Wrapped up a Grid by a StackPanel container? If yes - try to remove.
- Wrapped up a Grid by an external ScrollViewer control? If yes - try to remove.
One more point, could you bind whole items collection at once instead of adding each item into the grid.Items collection?
Solution 2:
A general tip for DataGrid
performance issues: I had a problem with the DataGrid in which it took literally seconds to refresh after a window resize, column sort, etc. and locked up the window UI while it was doing so (1000 rows, 5 columns).
It came down to an issue (bug?) with the WPF sizing calculations. I had it in a grid with the RowDefinition
Height="Auto"
which was causing the rendering system to try and recalculate the size of the DataGrid at runtime by measuring the size of each and every column and row, presumably by filling the whole grid (as I understand it). It is supposed to handle this intelligently somehow but in this case it was not.
A quick check to see if this is a related problem is to set the Height
and Width
properties of the DataGrid to a fixed size for the duration of the test, and try running again. If your performance is restored, a permanent fix may be among these options:
- Change the sizes of the containing elements to be relative (*) or fixed values
- Set
MaxHeight
andMaxWidth
of the DataGrid to a fixed value larger than it could get in normal use - Try another container type with different resizing strategy (
Grid
,DockPanel
, etc)