在C#winForm中一次删除多个实体

时间:2014-11-13 14:38:46

标签: c# winforms datagridview

我使用以下代码在winForm应用程序中通过datagridview从实体模型中删除记录。

var context= new AdminEntites();
foreach (DataGridViewRow row in dgvLoadTable.Rows)
    {
        if (row.Cells["chkcol"].Value != null && row.Cells["chkcol"].Value.Equals(true)) 
        {

            var selectedRec = (FILTER)this.rtBindingSource.Current;
                     //FILTER is the table name
                 context.DeleteObject(selectedRec);
                 context.SaveChanges();
          }
      }

此代码一次只删除一行。如何一次删除所有选定的行?

1 个答案:

答案 0 :(得分:0)

工作代码是:

foreach (DataGridViewRow row in this.dgvLoadTable.SelectedRows)
 {
    if (row.Cells["chkcol"].Value != null && row.Cells["chkcol"].Value.Equals(true)) 
    rtBindingSource.RemoveAt(row.Index);
  }//end of foreach
context.SaveChanges();
相关问题