views:

1498

answers:

3

Hi,

I have a performance problem with the (WPF Toolkit) DataGrid. It contains about 1.000 rows (only eight columns) and scrolling is horribly slow and laggy. Also the initial load of the Window containing the DataGrid takes 5-10 seconds.

I did some research (using google and StackOverflow) but couldn't find anything besides the advice to turn on UI virtualization. But even after explictly enabling that scrolling continues to be awfully slow.

My DataGrid is bound to an ICollectionView / CollectionViewSource. It's is defined in XAML like this (the columns are explicitly defined, not auto generated):

    <tk:DataGrid x:Name="dataGrid" 
                 ItemsSource="{Binding Path=Bookings}" 
                 AutoGenerateColumns="False" 
                 Grid.Row="1" 
                 EnableRowVirtualization="True" 
                 EnableColumnVirtualization="True"
                 VirtualizingStackPanel.IsVirtualizing="True"
                 VirtualizingStackPanel.VirtualizationMode="Recycling">
            ... 
    </tk:DataGrid>

The DataContext for the entire Window is set to an instance of the class containing the ICollectionView the DataGrid is bound to.

Every blog or forum post I found was praising the DataGrid's performance so I'm quite obviously doing something seriously wrong. Since I'm quite new to WPF in general and especially to the DataGrid I've no clue of how to improve this. Does anybody have some advice for me? What's your experience with the DataGrid? What am I doing wrong?

Edit: Just followed this question's advice to set the Width of all columns to "Auto". That did not change the bad scrolling performance. Also I'm not using DataGridTemplateColumns (just some DataGridTextColumns and two DataGridComboBoxColumns).

Edit2: I used Snoop to look at my app. What I see suggests that virtualization is indeed working (only 19 rows, not a thousand). But every row contains 52 elements, so those add up to more than thousand elements. Might that be a / the problem?

Thanks a lot!

+2  A: 

What container does your datagrid live in? For example - if you put it in a scrollviewer, the datagrid will grow to display every row, thus effectively disabling virtualization (and the scrollviewer will make it appear normal while this happens). Make sure that the datagrid size is bounded.

It really does sound like a virtalization thing, if this advice doesn't work run your app through a profiler to make sure virtualization is happening.

Edit: Here is an example of how to use snoop (or mole I guess) to quickly see if the virtualization is working. http://blogs.msdn.com/jgoldb/archive/2008/03/25/quick-tips-to-improve-wpf-app-memory-footprint.aspx

Egor
Sorry I forgot to mention that: the DataGrid lives inside a Grid which itself lives inside a Window. The RowDefinition for the DataGrid has it's Height set to "*".
andyp
Just set the Height of the DataGrid to some explicit value. That didn't change the anything. Did you mean that by "Make sure that the datagrid size is bounded."?
andyp
Yes I did, problem is clearly something else I suppose.
Egor
Thanks for the link. I updated the question to include my findings with Snoop (seems virtualization works just fine). Do you have any other ideas?
andyp
+2  A: 

Anyone further ideas on how to solve this? I'm experiencing the same issue - but I only have 50 or so rows.. fair enough there are about 40 columns and most are templated, but come on.. Excel on my 386 ran better than this!!

I can confirm row virtualization is on, verified through snoop. I also turned column virtualization on but that just made things worse, there were awful rendering defects for a few seconds until it caught up.

I have also run the performance toolkit but that was next to useless, told me the DataGridRowsPresenter was eating up all the resource (which was obvious anyway).

Bone
A: 

You could try adding the items one by one (or row by row) in the datagrid and updating the UI thread after each addition. That way, the user sees the loading take place and it doesn't seem like the application is doing nothing. See here a more detailed description of this method

AlexC
Thanks for you input. Your suggestion might help with the initial load but that's only a secondary problem. My real problem is the laggy scrolling of the grid.
andyp