I'm using a .NET 4.0 WPF DataGrid
, bound to an ObservableCollection<T>
. The collection is having rows added and deleted a few times a second. The DataGrid
is contained in a TabControl
/ TabItem
/ Grid
.
It works more-or-less OK as long as I leave it visible. If I go to a different tab in the app, then come back a few minutes later, the UI thread locks up for 30 seconds (with about 1200 total rows, about 40 of which are on the screen). All row data is already in memory, so no I/O. All bound properties are either simple strings, numbers or dates, or the logic to create them is very simple; nothing complex or time consuming to calculate.
When there are more than a few hundred rows, selecting a new column to sort by is very slow. Adding a new row also seems slow. If I switch to another tab and then switch right back, the grid reappears quickly.
Resizing the window performs fine.
Scrolling performance is slow at first, but gets after the grid is fully loaded; it's never great. Without ScrollView.CanContentScroll="False"
, scrolling is so slow as to be unusable. Using IsDeferredScrollingEnabled="True"
instead results in a 10+ second delay in rendering after the user lets go of the scrollbar -- still an unacceptable user experience.
There are 17 DataGridTextColumns
. Several of the columns use custom StringFormats
, but nothing complex. No TypeConverters
.
The Visual Studio performance tools/profiler have been useless. This seems like a relatively simple/straightforward setup. Any suggestions on how to improve performance would be appreciated.
I'd also like to know WHY it's SO slow.
<DataGrid x:Name="MyGrid" AutoGenerateColumns="False" Margin="3,35,3,20" VerticalContentAlignment="Center" Width="Auto"
FontSize="12" FontFamily="Consolas" ScrollViewer.CanContentScroll="False"
CanUserResizeRows="False" AlternationCount="2" AlternatingRowBackground="#FFE3F0FF"
VirtualizingStackPanel.VirtualizationMode="Recycling" IsReadOnly="True">
<DataGrid.Columns>
<DataGridTextColumn Header="XX" Binding="{Binding Path=XX}" />
<DataGridTextColumn Header="YY" Binding="{Binding Path=YY, StringFormat={}{0:0.0}}">
<DataGridTextColumn.ElementStyle>
<Style>
<Setter Property="TextBlock.TextAlignment" Value="Right" />
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
. . .
</DataGrid.Columns>
</DataGrid>