title: WPF DataGrid custom sort: reorder items in ItemsSource
body: <p>I have my custom grid which inherits from <code>DataGrid</code> (from WPFToolkit) that has around 10000 items in it. The built-in sort is very slow. As such I have written a separate class that keeps a running sort of all the <code>DataRowView</code> items for each column (this works because additions and removals from the grid are extremely seldom, if ever). </p>
  
  <p>The grid has <code>AutoGenerateColumns='True'</code> and is bound to the <code>DefaultView</code> of a <code>DataTable</code>. </p>
  
  <p>I override the <code>OnSorting</code> to know when a column header is clicked and try to replace the <code>ItemsSource</code> of the grid with my sorted list of DataRowView. Below is the method:</p>
  
  <pre><code>    private void RefreshItems()
      {
          if (_updating || _multiIndexer.Count == 0)
              return;
  
          try
          {
              _updating = true;
              this.AutoGenerateColumns = false;
  
              // replace the itemssource with my maintained and sorted list of 
              // DataRowView items
              this.ItemsSource = _multiIndexer.ToList();
          }
          finally
          {
              //this.AutoGenerateColumns = true;
              _updating = false;
          }
      }
  </code></pre>
  
  <p>The problem is that I destroy the columns that existed from the auto-generation. Also, I'm only left with columns that match the properties from <code>DataRowView</code>. </p>
  
  <p>I believe the best approach would be to create a <code>DataView</code> from my sorted list of <code>DataRowView</code> and pass that to the <code>ItemsSource</code> but I have yet to have any success.</p>
  
  <p>Any ideas how to pass a new list of rows to the ItemsSource or Items without destroying the auto-generated columns? Generating all my columns manually is not an option.</p>
  
  <p>Cheers,
  Sean</p>