title: How to select a DataGrid row in WPF with MVVM when the DataGrid is bound to a DataTable
body: <p>I have successfully bound a <code>DataTable</code> to a <code>DataGrid</code> control in <code>WPF with MVVM</code>. (I have defined the DataTable in the <code>viewmodel</code>.)</p>
  
  <p>Then I have defined a <code>DataRowView</code> type property and bound to the <code>SelectedItem</code> property of the DataGrid control.</p>
  
  <p>I can get the selected item through that. But I tried to set the selected item but I couldn't find a way to do it. Can somebody help me to figure it out.</p>
  
  <p>The <code>view</code></p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;Window x:Class="Pivot.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:vm="clr-namespace:Pivot.ViewModels"
      Title="MainWindow" Height="350" Width="525"&gt;
  &lt;Grid&gt;
  
      &lt;Grid.DataContext&gt;
          &lt;vm:MainViewModel /&gt;
      &lt;/Grid.DataContext&gt;
  
      &lt;DataGrid 
          ItemsSource="{Binding SizeQuantityTable}"
          AutoGenerateColumns="True"
          SelectedValue="{Binding SelectedRow}" 
          Margin="0,0,0,120" /&gt;
  &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>The <code>View Model</code></p>
  
  <pre class="lang-cs prettyprint-override"><code>public class MainViewModel : ViewModelBase
  {
      #region Declarations
  
      private DataTable sizeQuantityTable;
      private DataRowView selectedRow;
  
      #endregion
  
      #region Properties
  
      /// &lt;summary&gt;
      /// Gets or sets the size quantity table.
      /// &lt;/summary&gt;
      /// &lt;value&gt;The size quantity table.&lt;/value&gt;
      public DataTable SizeQuantityTable
      {
          get
          {
              return sizeQuantityTable;
          }
          set
          {
              sizeQuantityTable = value;
              NotifyPropertyChanged("SizeQuantityTable");
          }
      }
  
      /// &lt;summary&gt;
      /// Gets or sets the selected row.
      /// &lt;/summary&gt;
      /// &lt;value&gt;The selected row.&lt;/value&gt;
      public DataRowView SelectedRow
      {
          get
          {
              return selectedRow;
          }
          set
          {
              selectedRow = value;
              NotifyPropertyChanged("SelectedRow");
          }
      }
  
      #endregion
  
      #region Constructors
  
      /// &lt;summary&gt;
      /// Initializes a new instance of the &lt;see cref="MainViewModel"/&gt; class.
      /// &lt;/summary&gt;
      public MainViewModel()
      {
          this.SizeQuantityTable = new DataTable();
  
          DataColumn sizeQuantityColumn = new DataColumn();
          sizeQuantityColumn.ColumnName = "Size Quantity";
          this.SizeQuantityTable.Columns.Add(sizeQuantityColumn);
  
          DataColumn sColumn = new DataColumn();
          sColumn.ColumnName = "S";
          this.SizeQuantityTable.Columns.Add(sColumn);
  
          DataColumn mColumn = new DataColumn();
          mColumn.ColumnName = "M";
          this.SizeQuantityTable.Columns.Add(mColumn);
  
          DataRow row1 = this.SizeQuantityTable.NewRow();
          row1[sizeQuantityColumn] = "Blue";
          row1[sColumn] = "12";
          row1[mColumn] = "15";
          this.SizeQuantityTable.Rows.Add(row1);
  
          DataRow row2 = this.SizeQuantityTable.NewRow();
          row2[sizeQuantityColumn] = "Red";
          row2[sColumn] = "18";
          row2[mColumn] = "21";
          this.SizeQuantityTable.Rows.Add(row2);
  
          DataRow row3 = this.SizeQuantityTable.NewRow();
          row3[sizeQuantityColumn] = "Green";
          row3[sColumn] = "24";
          row3[mColumn] = "27";
          this.SizeQuantityTable.Rows.Add(row3);
  
          DataRow row4 = this.SizeQuantityTable.NewRow();
          row4[sizeQuantityColumn] = "Yellow";
          row4[sColumn] = "30";
          row4[mColumn] = "33";
          this.SizeQuantityTable.Rows.Add(row4);
  
      }
  
      #endregion
  }
  </code></pre>