数据表+删除一行在c#使用循环 [英] Data Table + delete a row in c# using loop

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

问题描述

我有一个数据表,我想删除一行这里是我的代码,这是我抛出一个例外

  foreach( DataRow row in dt1.Rows)
{
if((row [Name] ==Select a Lookbook)||(row [Name] ==创建新Lookbook ))
{
row.Delete();
dt1.AcceptChanges();
}
}

我甚至尝试在if和抛出我的错误任何想法如何实现这个任务这是我得到的例外:

 集合已修改;枚举操作可能无法执行。 

最终工作代码:

  foreach(dt1.Select()中的DataRow行)
{
if((row [Name] ==Select a Lookbook)||(row [名称] ==创建新Lookbook))
{
row.Delete();
}

}


解决方案

而不是使用 dt1.Rows ,使用 dt1.Select()



这里的目标不是使用集合本身,而是一行不是行的数组集合


I have a data table and I want to delete a row here is my code it's throwing me an exception

foreach (DataRow row in dt1.Rows)
{
    if ((row["Name"] == "Select a Lookbook") || (row["Name"] == "Create a new Lookbook"))
    {
        row.Delete();
        dt1.AcceptChanges();
    }
}

I even tried outside the if statment and outside forloop still throws me error any idea how to achieve this task this is the exception I get:

Collection was modified; enumeration operation might not execute.

Final working Code:

foreach (DataRow row in dt1.Select())
{
    if ((row["Name"] == "Select a Lookbook") ||    (row["Name"] == "Create a new Lookbook"))
    {
        row.Delete();                                       
    }

}

解决方案

Instead of using dt1.Rows, use dt1.Select()

The goal here is not to use the collection itself, but rather an array of row that is not the Rows collection

这篇关于数据表+删除一行在c#使用循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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