Very slow WPF datagrid performance

Dec 10, 2008 at 3:55 PM
Hello,

I have a datagrid that has about 2000 rows and I see terrible lag issues when selecting items in the grid.  Am I doing something wrong below?

I used WPF Perf to test and everything seems ok from a screen drawing perspective. I also replaced the DataGrid with a listview and the listview is much faster than the datagrid.

Main issues:
  • The selected row in the grid lags behind when scrolling up and down with the arrow keys.  If hold down and then let off of the arrow key, rows keep getting selected for a few seconds (because of the lag).
  • Selecting a single item with the mouse that is in view on the grid (no scrolling) shows a noticeable lag for the item to become selected ( I can count to about a second saying "one-thousand one"). 
Below, I have created a very simple test project that shows this performance issue.  Am I missing a setting for virtualization to kick in?

Thank you for any help!
Jon

XAML

<Window x:Class="WpfDataGridPerfTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:tk="http://schemas.microsoft.com/wpf/2008/toolkit"
    Title="Window1" Height="500" Width="800"
         Loaded="Window_Loaded">

        <tk:DataGrid x:Name="grid" SelectionUnit="FullRow"
                AutoGenerateColumns="False"   Background="Azure"
                CanUserAddRows="False" CanUserDeleteRows="False"
                EnableRowVirtualization="True" EnableColumnVirtualization="True">
            <tk:DataGrid.Columns>

                <tk:DataGridTextColumn Header="Col1"  Binding="{Binding Col1}" IsReadOnly="True" />
                <tk:DataGridTextColumn Header="Col2"  Binding="{Binding Col2}" IsReadOnly="True" />
                <tk:DataGridTextColumn Header="Col3"  Binding="{Binding Col3}" IsReadOnly="True" />
                <tk:DataGridTextColumn Header="Col4"  Binding="{Binding Col4}" IsReadOnly="True" />
                <tk:DataGridTextColumn Header="Col5"  Binding="{Binding Col5}" IsReadOnly="True" />
                <tk:DataGridTextColumn Header="Col6"  Binding="{Binding Col6}" IsReadOnly="True" />
                <tk:DataGridTextColumn Header="Col7"  Binding="{Binding Col7}" IsReadOnly="True" />
                <tk:DataGridTextColumn Header="Col8"  Binding="{Binding Col8}" IsReadOnly="True" />
                <tk:DataGridTextColumn Header="Col9"  Binding="{Binding Col9}" IsReadOnly="True" />
                <tk:DataGridTextColumn Header="Col10"  Binding="{Binding Col10}" IsReadOnly="True" />
                
            </tk:DataGrid.Columns>
        </tk:DataGrid>

</Window>


CODE BEHIND

namespace WpfDataGridPerfTest
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            List<TestEntity> items = new List<TestEntity>();
            
            for (int i = 0; i < 2000; i++)
            {
                items.Add(new TestEntity());
            }
            grid.ItemsSource = items;
        }
    }
}

TEST ENTITY
namespace WpfDataGridPerfTest
{

    public class TestEntity
    {
        public TestEntity()
        {
            Col1 = Guid.NewGuid().ToString();
            Col2 = Guid.NewGuid().ToString();
            Col3 = Guid.NewGuid().ToString();
            Col4 = 4;
            Col5 = 5;
            Col6 = 6;
            Col7 = 7;
            Col8 = 8;
            Col9 = 9;
            Col10 = 10;

        }
        public string Col1 { get; set; }
        public string Col2 { get; set; }
        public string Col3 { get; set; }
        public int Col4 { get; set; }
        public int Col5 { get; set; }
        public int Col6 { get; set; }
        public int Col7 { get; set; }
        public int Col8 { get; set; }
        public int Col9 { get; set; }
        public int Col10 { get; set; }
    }
}
Dec 15, 2008 at 1:15 PM
Hi,

Are there known issues with performance with the DataGrid?  I have Premiere Microsoft Development Support hours that I can use on this issue, but I would prefer to not have to go through several days with them to get this all sorted just to come to an answer "that is how it is right now".

I can also shoot a video of what I am seeing if that would help...

Thanks,
Jon
Coordinator
Dec 15, 2008 at 8:19 PM
Hi Jon,

I tried out your test project, but I'm not able to repro the problem.  On my machine, the selection happens instantly with the mouse and I'm not seeing any issues with scrolling.  The only known issues we have with performance is with SelectAll() performance - you can read more about this here: http://www.codeplex.com/wpf/Wiki/View.aspx?title=SelectAll%28%29%20Performance&referringTitle=Tips%20%26%20Tricks.  (We also have a known issue with sort performance, but that doesn't come into play in this scenario.)

I'm not sure what else could be causing this problem, except maybe if you're using a slower machine?  I'll check with the team to see if anyone has any other ideas.  You've already got both row and column virtualization turned on and in this simple app, there shouldn't be anything interfering with the virtualization, as far as I know.  I'll let you know if the team has any other comments.

Thanks,
Samantha
Coordinator
Dec 15, 2008 at 10:02 PM

Can you provide some information about the machine on which you're running the test application?

  • Operating System (XP, Vista, Windows 7 Beta)
  • Configuration (Desktop, Laptop, Tablet)

Thanks,
Ben

Dec 16, 2008 at 1:01 AM
Edited Dec 16, 2008 at 2:23 AM
Hi Ben,

Here are the specifics of the machine:

Vista Enterprise x64
HP Desktop Workstation (ws xw4600) http://h10010.www1.hp.com/wwpc/us/en/sm/WF06a/12454-12454-296719-307907-296721-3429268.html
Intel Core2 Duo E8500 - 3.16ghz
8 GB RAM
NVidia Quadro FX 1700 Video (I just downloaded the latest driver from HP)

Samsung SyncMAster 30" monitor running at 2560x1600

I'm going to follow up with a video...
Jon



Dec 16, 2008 at 1:57 AM
Edited Dec 16, 2008 at 2:22 AM
Hi Ben and Samantha,

Here is a video showing the issues: http://www.jonkragh.com/video/gridperf2.wmv

Thanks for your help!
Jon

EDIT: Here is the test project I used in the video
http://www.jonkragh.com/video/gridperf.zip
Dec 17, 2008 at 2:13 PM
OK - I think I found it - I uninstalled a Wacom USB Tablet driver from my system and now the grid is FAST!

Is this a tablet issue in general, or just specific to something with the Wacom?

Thanks,
Jon



Coordinator
Dec 17, 2008 at 7:07 PM

I believe the issue arises only on tablets, but I haven't yet investigated the root cause. Will keep you posted.

Ben

Dec 17, 2008 at 7:28 PM

Cool Ben!  I am not using the tablet in production, I was actually just using it to draw up some design whiteboard stuff.

Anyway, I would be interested to hear more about this.  What a relief at least at this point that I can get good performance for my production app - which is a real time high performance app that I am developing (constant background updates of the grid, etc).

Cheers,
Jon

Dec 18, 2008 at 8:48 AM
Hi,

I also recognized that WPF controls are very performance intensive.
So I tried out your project against a Win32 listview.
The application with the Win32 listview uses 3% of the CPU the WPF application uses >30% with GridView and >40% with DataGrid while pressing the arrow key continuously down.
The CPU usage is much higher when pressing the page down key.
 
So, I decided to use the nice WPF datagrid only for small amount of items.

WorstCase
Jan 7, 2009 at 12:40 AM
Hi, I relise this is a bit old, but I'd like to add to this discussion.

When I was testing the Datagrid on my Tablet PC the performance is terrible, but on my Computer it is fine, it has to be something to do with Stylus input.

Has an Issue been logged for this?

Ben
Jan 10, 2009 at 6:27 PM
The same situation happened with me after removing my Genius Tablet Drivers every thing went back to speed .... it is definitely related to tablets...
Coordinator
Jan 12, 2009 at 10:40 PM
Thanks for your patience. I wanted to update you on what I have found so far.

After profiling the application, I see an extraordinary amount of calls to DataGridColumn.OnCopyingCellClipboardContent. This method is usually used for each cell to provide data to be placed on the clipboard when copying. In this case, however, the DataGridCellItemAutomationPeer's GetNameCore method is using the clipboard content as the name since a cell's name is rarely set. I presume, this is the name that is displayed in an automation tool, and the content of a cell is more useful in this case. However, this has the result that whenever the automation tree is updated, which happens after layout, every cell that is in view is providing data to the automation peer. This causes a lot of data binding to occur, and generally this is why it is slow.

I will follow up on two things now. The first is to find out why being on a Tablet PC causes these automation peers to instantiate, since normally they do not unless you are using an accessibility aid or an automation tool. The second is to figure out a better way to implement GetNameCore.

Thanks,
Ben

Apr 21, 2009 at 3:24 PM
Hi Ben,

Thanks for the follow up - anything new on this?

Cheers,
Jon
Coordinator
Apr 22, 2009 at 4:01 AM
In the March 2009 Toolkit release, DataGridCellItemAutomationPeer's GetNameCore method was changed to "Item: <your item's ToString()>, Column DisplayIndex: <column index>." This is much cheaper to create than the clipboard data and should resolve the issue.

The Tablet PC Input Panel is, apparently, an automation client and causes WM_GETOBJECT messages to be sent. WPF responds to WM_GETOBJECT by creating an automation peer for the root element (usually a Window). Whenever layout changes, a list of automation peers are called, and this root object is in that list. Due to this call, the entire visual tree's automation tree is created -- that means all of the cells' automation peers are instantiated too. I still don't know why all of this is happening.

Ben
Apr 22, 2009 at 2:53 PM
Hi Ben -

Thanks so much, you are the man!  Great explanation, I hope I never have to care about those deep details :)

I will test this in a week or so - I don't want to mess with my upcoming production release by installing my tablet again.  (I am using March 2009 though for this release).

Cheers,
Jon

Jun 30, 2009 at 8:26 AM
Edited Jul 3, 2009 at 12:26 PM

Hi All,

I have the same problem. The CPU is very high (About 80% if need to scroll, 40% if no scroll) when I press down arrow. I am using the June 2009 release and not using the tablet pc. Any solution to overcome this problem?

My machine is:

Core 2 duo 7200 2.53Ghz

4 GB ram

Vista business

Tony

Jul 9, 2009 at 8:16 PM

Hello,

I've also the same problems with the June 2009 release on a fast PC. I've set-up (with help of a code example), a simple grid with 200 rows and 10 columns. Scrolling isn't fast. With this simple table it is not terrible slow. However, more sophisticated tables are even slower. The datagrid doesn't have the performance of an Excel sheet (and Excel has many advanced features). So, faster scrolling is definitely possible.

The problems arise in: (DataGridRowsPresenter : VirtualizingStackPanel).MeasureOverride. This method takes relatively a long time to complete (with my analysis software, it is by leaps the slowest method). I’ve Changed the specific class (with some minor modifications) to DataGridRowsPresenter : StackPanel. This results in faster scrolling up and down, but slower scrolling left and right. Weird.

This should definitely be resolved. The reason is Excel, even on slow machines Excel outperforms the datagrid with many advanced Excel features enabled. Why is WPF incapable of this?

Regards, Rick

 

Sep 1, 2009 at 4:07 PM

Hi,

I tried to post a new discussion about this issue : http://wpf.codeplex.com/Thread/View.aspx?ThreadId=67598

 

Apr 13, 2010 at 7:19 PM
Are there any updates in regards to the issue with the Datagrid performance on a tablet. I am experiencing very slow response times also on a very fast tablet. I have an old XP machine running the same application and the grid is faster on that machine than the tablet. I'm using WPFToolkit version 3.5.40128.1. Is that the latest version?
Apr 15, 2010 at 2:03 AM

Hi,

The version included in .net 4.0 does not seem to have this problem with tablets from what I can tell.

I installed it on Monday on the machine that had issues and it seems to be working ok.

Cheers,
Jon

Jul 26, 2010 at 8:03 AM

I have similar problem with data grid. But I automated Ctrl + Shift + End on "Select All" or "Ctrl + A" on the data grid and selection mode improved a lot. De-virtualisation is the key, I guess.