title: Show row index and reset sorting
body: <p>I am a WPF-noob and have two DataGrid problems:</p>
  
  <p><strong><em>Problem 1:</em></strong></p>
  
  <p><strong>What I got:</strong>
  A DataGird that gets its data from a array:</p>
  
  <pre><code> &lt;DataGrid x:Name="dgMain" ItemsSource="{Binding TestArray}" Margin="0,20,0,0" LoadingRow="dgMain_LoadingRow"&gt;
              &lt;DataGrid.Columns&gt;
                  &lt;DataGridTextColumn Header="Name" Binding="{Binding Name}"/&gt;
                  &lt;DataGridTextColumn Header="Address" Binding="{Binding Address}"/&gt;
                  &lt;DataGridTextColumn Header="Age" Binding="{Binding Age}"/&gt;
              &lt;/DataGrid.Columns&gt;
   &lt;/DataGrid&gt;
  </code></pre>
  
  <p>I am using the "LoadingRow" event to update the row header:</p>
  
  <pre><code>private void dgMain_LoadingRow(object sender, DataGridRowEventArgs e)
      {
          e.Row.Header = (e.Row.GetIndex() + 1).ToString();
      }
  </code></pre>
  
  <p><strong>What I want:</strong> 
  It should show the index of the line in the row header.</p>
  
  <p><strong>Whats the problem?:</strong> If I add items or load items it works great, but if I sort the DataGird the index of the lines don't change.</p>
  
  <p><img src="https://i.stack.imgur.com/XZoOg.png" alt="unsorted">
  <img src="https://i.stack.imgur.com/wjXzc.png" alt="sorted"></p>
  
  <p>The left one is unsorted and the right one is sorted (by Name). In this example the index of line "F" should be 6 instead of 1 (right image).</p>
  
  <p>(Not related to the problem: Why are all RowHeaders selected?)</p>
  
  <p><strong><em>Problem 2:</em></strong></p>
  
  <p><strong>What I got:</strong> <em>see Problem 1</em></p>
  
  <p><strong>What I want:</strong> After I added a row it should jump to the next empty row.</p>
  
  <p><strong>Whats the problem?:</strong> If I sort the datagrid it jumps to the next line, even if it is not empty.</p>
  
  <p><img src="https://i.stack.imgur.com/ajJOC.png" alt="monkey"></p>
  
  <p>First I sorted the list by name and then I added "A: First", "B: Second", "C: Third" and then "A: Fourth" and because of the sorting "A: Fourth" automatically gets the second line and then it jumps to the third line instead to the last, empty line. </p>
  
  <p><strong>Edit:</strong> Sorry, I forgot to say: It should 1. reset the sorting and 2. jump to the last, empty line. The order should be "A: First", "B: S", "C: T" and "A: Fourth".</p>
  
  <p>I really hope there are easy solutions. Thanks :)</p>