简体   繁体   English

DataTable 已删除行问题

[英]DataTable Deleted Rows Issues

I have an app that we maintain that is years old.我有一个我们维护多年的应用程序。 Suddenly, rows deleted in a DataTable are showing up in the DataTable.突然,在 DataTable 中删除的行显示在 DataTable 中。 They are marked as deleted, but they show up in the Row.Count, and if I try to loop through the table rows, I get Cannot access deleted rows errors.它们被标记为已删除,但它们显示在 Row.Count 中,如果我尝试遍历表行,我会收到Cannot access deleted rows错误。 If I do an AcceptChanges() , the deleted records do disappear.如果我执行AcceptChanges() ,删除的记录会消失。

For example this line of code, where I had previously deleted all the rows, would error out with the cannot access error:例如,我之前删除了所有行的这行代码会因无法访问错误而出错:

foreach (DataRow rwUpdatedRefs in tblUpdatedRefs.Rows)

Does anyone have an idea why this would deleted records would start showing up?有谁知道为什么删除的记录会开始出现? Is there some setting I may have accidently set?是否有一些我可能不小心设置的设置?

A deleted DataRow doesn't disappear from the rows collection of a DataTable until you call AcceptChanges.在调用 AcceptChanges 之前,已删除的 DataRow 不会从 DataTable 的行集合中消失。 It has always worked in this way.它一直以这种方式工作。 If you loop over a collection of rows where thera are deleted rows you will get the exception mentioned in your question.如果您遍历其中删除行的行集合,您将得到问题中提到的异常。 There is no settings to 'hide' those rows.没有设置可以“隐藏”这些行。

However, if you still want to loop over the rows collection without calling AcceptChanges on the DataTable you could still execute the loop excluding the Deleted Rows with:但是,如果您仍然想循环遍历行集合而不在 DataTable 上调用 AcceptChanges,您仍然可以执行不包括已删除行的循环:

foreach(DataRow rwUpdatedRefs in tblUpdatedRefs.Rows.Cast<DataRow>()
                                 .Where(r => r.RowState != DataRowState.Deleted))

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

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