删除数据表中的特定行 [英] Deleting specific row in datatable

查看:110
本文介绍了删除数据表中的特定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

if (NSFwithExtOrSerFound)
                     {
                         int count = 0;
                       foreach (DataRow recRow in dtRec.Rows)
                         {
                             if (recRow[0].ToString() == "Contact Vibration Specialist")
                             {
                                 count++;
                             }
                         }

                       foreach (DataRow recRow in dtRec.Rows)
                       {
                           if (recRow[0].ToString() == "Contact Vibration Specialist")
                           {
                               count--;
                               if (count > 0)
                               {
                                   recRow[0] = string.Empty;
                                   recRow.Delete();
                               }
                           }
                       }
                     }



我正在删除数据表中的特定行但是


I am deleting a specific row in the datatable but at

recRow.Delete();

我收到的错误如 [System.InvalidOperationException] = {收集已被修改;枚举操作可能无法执行。}

推荐答案

您好



一条规则 FOREACH 循环就是这样,你无法修改循环集合。所以尝试使用 FOR 循环。



试试这个。



Hi

One rule with FOREACH loop is that, you cannot modify the loop collection. So try using FOR loop.

try this.

for(int i=0; i < dtRec.Rows.Count; i++)
{
   DataRow recRow = dtRec.Rows[i];
   if (recRow[0].ToString() == "Contact Vibration Specialist")
   {
       count--;
       if (count > 0)
       {
           recRow[0] = string.Empty;
           recRow.Delete();
           dtRec.AcceptChanges();
       }
   }
}





希望它有所帮助。



hope it helps.


您需要以下列格式更改删除语句

You will need to change the delete statement in the following format
dt.Rows.Remove(dr);
dt.AcceptChanges();





其中dt是你要删除行和数据的数据表是需要删除的特定行。



where dt is the data table from where you would like to delete the row and dr is the particular row which needs to be deleted.


这篇关于删除数据表中的特定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆