I was developing a WPF application that contained a DataGrid like so many times in the past. However, this time it was taking 20-30 seconds for the DataGrid to render. I checked the query execution speed. I was building the DataTable dynamically, but I checked the speed of that too and it was fast as expected.
I compared my current DataGrid configuration with previous some previous code and found nothing. Column or Row virtualization was not the cause either. I compared configuration to configuration and a DataGrid in one WPF application configured exactly the same in another was simply performing 30-40 times slower.
What I found the issue to be was in my Grid.RowDefinitions setting. In the current WPF program I had this setting.
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
And in the WPF that had no performance problem I had this setting.
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
My DataGrid was on Row 2 and changing the Height from Auto to * resolved the performance problem.
How very simple it seems but was a challenge to find the solution.
Why this happens? What I saw when the DataGrid RowDefinition was set to Auto was that the DataGrid rendered a visible row for each row in my result set. When the RowDefinition was set to* there was a scrollbar and the DataGrid was probably able to use Row Virtualization. Check out my other article about virtualization in WPF here.