简体   繁体   English

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

[英]Deleting multiple entities at once in C# winForm

I am using the following code to delete records from entity model by datagridview in a winForm application. 我正在使用以下代码通过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();
          }
      }

This code is only deleting a single row one at a time. 此代码一次只删除一行。 How can I delete all the selected rows at once? 如何一次删除所有选定的行?

The working code is: 工作代码为:

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM