数据表+在c#中使用循环删除一行 [英] Data Table + delete a row in c# using loop
本文介绍了数据表+在c#中使用循环删除一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据表,我想删除一行,这是我的代码,它向我抛出异常
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();
}
}
我什至在 if 语句之外尝试过,在 forloop 之外仍然抛出错误任何想法如何实现此任务这是我得到的异常:
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.
最终工作代码:
foreach (DataRow row in dt1.Select())
{
if ((row["Name"] == "Select a Lookbook") || (row["Name"] == "Create a new Lookbook"))
{
row.Delete();
}
}
推荐答案
不要使用 dt1.Rows
,而是使用 dt1.Select()
Instead of using dt1.Rows
, use dt1.Select()
这里的目标不是使用集合本身,而是使用不是 Rows
集合的行数组
The goal here is not to use the collection itself, but rather an array of row that is not the Rows
collection
这篇关于数据表+在c#中使用循环删除一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文