title: How do I delete selected rows in DataGrid?
body: <p>I am trying to delete selected rows in a dataGrid when they hit the delete button. I want it to delete from the Database and the DataGrid.</p>
  
  <p>here is how I populate the Gridbox, using Entity Framework </p>
  
  <pre><code> ObjectContext objectContext3 = ((IObjectContextAdapter)dataEntities).ObjectContext;
                  ObjectSet&lt;STUDENT&gt; students = objectContext3.CreateObjectSet&lt;STUDENT&gt;();
                      var query4 =
                      from STUDENT in students
                      orderby STUDENT.NameID
                      select new { STUDENT.DOB, STUDENT.NameID };
                      userDataGrid.ItemsSource = query4.ToList();
                      userDataGrid.UpdateLayout();
  </code></pre>
  
  <p>Here is what I have attempted to do for deleting it. I am not 100% sure if this is even correct. If you think there is a better way, Please let me know. </p>
  
  <pre><code>private void deleteRow_Click(object sender, RoutedEventArgs e)
      {
          DataGrid dg = this.userDataGrid;
  
          if (dg != null)
          {
              var toDelete = (IList)dg.SelectedItems;//&lt;-- Datagrid.SelectedItems
              var collection = toDelete.Cast&lt;DataRowView&gt;();
              var list = new List&lt;DataRowView&gt;(collection);
  
              foreach (var item in list)
              {
                  item.Row.Delete();
              }
          }
      }
  </code></pre>
  
  <p>Any help will be greatly appreciated!</p>