title: How to delete selected rows (using checkbox) in wpf datagrid
body: <p>My WPF DataGrid is:</p>
  
  <pre><code>&lt;dg:DataGrid Name="datagrid1"  Grid.RowSpan="1"  VerticalAlignment="Stretch" Grid.ColumnSpan="2"&gt;
  
      &lt;dg:DataGrid.Columns &gt;
          &lt;dg:DataGridTemplateColumn&gt;
              &lt;dg:DataGridTemplateColumn.Header&gt;
                  &lt;CheckBox Content=" Slect All" x:Name="headerCheckBox" /&gt;
              &lt;/dg:DataGridTemplateColumn.Header&gt;
              &lt;dg:DataGridTemplateColumn.CellTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;CheckBox Name="chkSelectAll" Margin="45 2 0 0"
        IsChecked="{Binding IsChecked, ElementName=headerCheckBox, 
                            Mode=OneWay}" /&gt;
                  &lt;/DataTemplate&gt;
              &lt;/dg:DataGridTemplateColumn.CellTemplate&gt;
          &lt;/dg:DataGridTemplateColumn&gt;
  
      &lt;/dg:DataGrid.Columns&gt;
  &lt;/dg:DataGrid&gt;
  </code></pre>
  
  <p>Also Dynamicaly I am populating the data to the datgrid.In xaml.cs file I written the below given code for deleting the selected row from the data grid but it throwing the error at line</p>
  
  <pre><code>DataGridRow item =(DataGridRow) datagrid1.ItemContainerGenerator.ContainerFromItem(datagrid1.Items[j]);
  </code></pre>
  
  <p>So Please have a look in to the below given code which I written for doing the same.</p>
  
  <pre><code>   private void Button_Click_1(object sender, RoutedEventArgs e)
      {
          for (int j = 0; j &lt; datagrid1.Items.Count; j++)
          {
              DataGridRow item =(DataGridRow) datagrid1.ItemContainerGenerator.ContainerFromItem(datagrid1.Items[j]);
              CheckBox ckb = (CheckBox)GetVisualChild&lt;CheckBox&gt;(item);
              if (ckb.IsChecked.Value)
              {
                  DataRowView drv = (DataRowView)datagrid1.Items[j];
                 //delete the row- updating to the database
              }
          }
      }
      static T GetVisualChild&lt;T&gt;(Visual parent) where T : Visual
      {
          T child = default(T);
          int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
          for (int i = 0; i &lt; numVisuals; i++)
          {
              Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
              child = v as T;
              if (child == null)
              {
                  child = GetVisualChild&lt;T&gt;(v);
              }
              if (child != null)
              {
                  break;
              }
          }
          return child;
      } 
  </code></pre>
  
  <p>Please let me know if am wrong.</p>