We are struggling with the slowness of the datagrid in wpf. Regardless of which collection we are using - List, BindingList, ObservableCollection, custom ObservableCollection, it's still very slow when responding to collection update (Clear, Add) and render on the screen.
The fastest solution we found is not to update the collection, but update the exsting objects in the collection, that DataGrid is bound to. In this case the grid is responding very fast, like old winforms grid.
We are creating 65535 rows in the grid, that will be enough for all our grids. When it comes to update the grid, we update the required number of top rows, and set visibility = hidden for the rest. As I said it works exteremely fast. But There are 2 issues that we cannot solve:
- scroll bar, since we don't collapse the rows (it's slow) - it's always is set for 65535 row. Is there any way to limit the scroll, or grid size to the actual visible rows count?
Also, I noticed, that adding new rows into collection, if they don't need to be rendered immediately (they will be out of the visible area) is also very fast, so we can limit the minimum set to 50 rows (max visible rows), and then add/remove new rows as required. But this does not solve the problem with the scrolling.
Any other solutions are welcome. Foreseeing the advise to enable virtualization - yes, we are using virtualization (for rows which are enabled by default, and for columns).
Update:
We are trying to display 20 columns x 50 rows of data. If we modify the source collection with Clear() and then Add(), the rendering time is about 1 second, which is not acceptable at all for us, as the UI is frozen for a second. I tried to resize the datagrid to 0 Height, and then set size back incrementally in the background thread, it unfreezes the UI, but overlook is ugly, and I haven't managed how to set datagrid back to fill the parent control. It seems after Height is set in code, there is no way back.
As in initial post, the alternate solution we found it not to modify the collection. Just limit the grid to 65535 rows, don't add or remove new items. It works really fast, but now we are facing problems in synchronizing scrolling, and sorting.
I found the datagrid is very frustrating control in WPF. The performance is below any reasonable limits.
What we are trying to achieve is responsiveness. It should not block UI more than several milliseconds, when loading the data.
XAML: nothing extraordinary
<DataGrid x:Name="TheGrid"
DockPanel.Dock="Top"
ItemsSource="{Binding Collection}"
EnableColumnVirtualization="True"
EnableRowVirtualization="True"
AutoGenerateColumns="False"
ColumnWidth="70"
RowHeight="20"
>
<DataGrid.Columns>
<DataGridTemplateColumn Header="Header 1" >
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{Binding Field1, Mode=TwoWay}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Field1}" /></DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn >
..... and so on for 20 columns
Also, the datagrid is not inside a StackPanel (this makes it terrible slow).