【问题标题】:Deleting multiple entities at once in C# winForm在 C# winForm 中一次删除多个实体
【发布时间】:2014-11-13 14:38:45
【问题描述】:

我正在使用以下代码通过 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();
          }
      }

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

【问题讨论】:

  • 这样做了,没用。
  • 我是 C# 新手,但看起来你的电话 rtBindingSource.Current 不是说最后选择的记录吗??
  • 是的,确实如此,这就是我在 foreach 循环中调用它的原因。无论如何,我得到了另一个解决方案,所以问题解决了。谢谢大家的帮助。
  • 你能告诉我们你是怎么解决的吗?它可能会帮助其他有同样问题的人。
  • @bodjo 是的 是的 是的。保存 NEWBS(像我一样。:-))

标签: c# winforms datagridview


【解决方案1】:

工作代码是:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-30
    • 1970-01-01
    相关资源
    最近更新 更多