title: WPF DataGrid select first row
body: <p>I am using a TabControl and a DataGrid on each tab.</p>
  
  <p>Only the first Tab shows the first row automatically. Each DataGrid is filled with a binding with SQL. When the DataGrid is empty, or when a field is not selected and the select button is clicked, then the program crashes.</p>
  
  <pre><code>IsSynchronized = on
  SelectionMode = Extended
  SelectionUnit = FullRow
  </code></pre>
  
  <p>I need the right statement for the select button. This is my code:</p>
  
  <pre><code>    private void Btn1_Click(object sender, RoutedEventArgs e)
      {
          var DG1 = dg1.SelectedCells[0];
          var cellInfor = dg1.SelectedCells[0];
          dg1.CurrentCell = new DataGridCellInfo(dg1.Items[0], dg1.Columns[0]);
          var DG11 = (cellInfor.Column.GetCellContent(cellInfor.Item) as TextBlock).Text;
  
          if (dg1 == null || dg1.CurrentCell == null)
          {
              MessageBox.Show("there is no recorid selected");
                  return;
          }
      }
  
      private void Btn2_Click(object sender, RoutedEventArgs e)
      {
          var DG2 = dg2.SelectedItems[0].ToString();
  
          if (DG2.Items[0].ToString() == null || DG2.SelectedItem[0] == null)
          {
              MessageBox.Show("There is no RecID number selected", "Error");
              return;
          }
  
  
          int RecID = Convert.ToInt32(DG22);
          //this.AptzClearControls();
          //this.GetAptzRecipe(RecID);
          //this.DisplayAptzRecipe();
  </code></pre>