title: wpf datagrid render performance (custom grid control)
body: <p>**Please see updates to question below</p>
  
  <p>I am trying to use a datagrid in my WPF application but I am finding the performance to be unbearable.</p>
  
  <p>I am binding to a dataset with only 100 rows and 15 columns and any sort of scrolling or even column width resizing paints EXTREMELY slowly.</p>
  
  <p>I recall the old winforms datagridview had poor performance when cell borders were turned on, but turning off gridlines in it's wpf counterpart has no effect.</p>
  
  <p>The Window with the grid:</p>
  
  <pre><code>&lt;Window x:Class="GridPerformanceTest.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="MainWindow" Height="350" Width="525"&gt;
  &lt;Grid&gt;
      &lt;DataGrid ItemsSource="{Binding}" AutoGenerateColumns="True" EnableColumnVirtualization="True" EnableRowVirtualization="True"
                VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Recycling" Background="{x:Null}" 
                BorderBrush="{x:Null}" IsSynchronizedWithCurrentItem="False" BorderThickness="0" RowHeight="15" 
                GridLinesVisibility="None" HorizontalGridLinesBrush="{x:Null}" VerticalGridLinesBrush="{x:Null}" ColumnHeaderHeight="15"/&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>How I am populating my data:</p>
  
  <pre><code>public partial class MainWindow : Window
  {
      public MainWindow()
      {
          InitializeComponent();
  
          DataSource source = new DataSource();
  
          DataSet ds = new DataSet();
          ds.Tables.Add(source.Execute("select * from tbl_users"));
  
          this.DataContext = ds.Tables[0];
      }
  }
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>So because I have had no luck with the datagrid, which I'm sure has many more features than what I am looking for right now (simply display plain text and select data for copy/paste) I decided to try and work on my own. </p>
  
  <p>This will be my first shot at a custom user control, but I have already found the source of the poor performance...the grid. </p>
  
  <p>If I write a control with grid splitters for my columns and stackpanels that contain my row data, scrolling through the rows is flawless - like I was used to with the good 'ol winforms datagrid. </p>
  
  <p>Grid control code:</p>
  
  <pre><code>public partial class CustomGridControl : UserControl
  {
      public CustomGridControl()
      {
          InitializeComponent();
  
          for (int i = 0; i &lt; 20; i++)
          {
              ColumnDefinition col = new ColumnDefinition();
              col.Width = new GridLength(75);
              _rootGrid.ColumnDefinitions.Add(col);
  
              StackPanel pnl = new StackPanel();
              pnl.Orientation = Orientation.Vertical;
  
              for (int x = 0; x &lt; 1000; x++)
              {
                  TextBlock blk = new TextBlock();
                  blk.Foreground = Brushes.Black;
                  blk.Text = "Row: " + x.ToString() + " Col: " + i.ToString();
  
                  pnl.Children.Add(blk);
              }
  
              Grid.SetColumn(pnl, i);
              _rootGrid.Children.Add(pnl);
  
              GridSplitter splitter = new GridSplitter();
              splitter.Width = 2;
              splitter.BorderBrush = Brushes.Black;
  
              Grid.SetColumn(splitter, i);
              Grid.SetRowSpan(splitter, 1000);
  
              _rootGrid.Children.Add(splitter);
  
          }
      }
  </code></pre>
  
  <p>And the XAML:</p>
  
  <pre><code>&lt;UserControl x:Class="CustomGrid.CustomGridControl"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
           xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
           mc:Ignorable="d" 
           d:DesignHeight="300" d:DesignWidth="300"&gt;
  
  &lt;Grid &gt;
      &lt;ScrollViewer&gt;
          &lt;Grid x:Name="_rootGrid" Background="Aqua"/&gt;
      &lt;/ScrollViewer&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>When I try and drag one of the splitters, the performance is horrible, just as with the standard WPF datagrid.</p>
  
  <p>Now I know I am not doing any virtualization to optomize performance or anything but the performance with stackpanels is still easily 100x better.</p>
  
  <p>To update my question - I would like to know if anyone has any ideas of how to handle the columns in another type of panel that would allow the same gridsplitter type functionality for the columns.</p>
  
  <p>Any good links to custom panels/grids would be much appreciated as well.</p>