vb.net 从DataTable中删除空行的方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13978526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Method to remove empty rows from DataTable?
提问by Richard Deeming
I parsed through an Excel spreadsheet and returned the entire result as a DataTable. However, this Excel spreadsheet has several empty rows that I would like to eliminate from the resulting DataTable. In particular, each empty row begins with an empty cell. So, I know that the entire row is empty if the value of the cell at the first index is empty. Note that I cannot simply modify the Excel spreadsheet because I have to work with exactly what the client has sent to me. Based on this information, I assumed that I could perform the following function to remove empty rows:
我解析了一个 Excel 电子表格并将整个结果作为 DataTable 返回。但是,这个 Excel 电子表格有几个空行,我想从生成的 DataTable 中删除这些行。特别是,每个空行都以一个空单元格开头。所以,我知道如果第一个索引处的单元格的值为空,那么整行都是空的。请注意,我不能简单地修改 Excel 电子表格,因为我必须准确处理客户发送给我的内容。基于这些信息,我假设我可以执行以下函数来删除空行:
' DataTable dt has already been populated with the data
For Each row As DataRow In dt.Rows
If dt.Rows.Item(0).ToString = "" Then
dt.Rows.Remove(row)
ElseIf dt.Rows.Item(0) Is Nothing Then
dt.Rows.Remove(row)
End If
Next
However, after crafting this solution, I am greeted with the following error:
但是,在制定此解决方案后,我遇到了以下错误:
Collection was modified; enumeration operation might not execute.
I now realize that I cannot alter the collection as I access it. How can I get around this? I'm wondering if I should create a new DataTable with the rows that aren't empty. Is this the best approach or are there better options?
我现在意识到我无法在访问它时更改它。我怎样才能解决这个问题?我想知道是否应该创建一个包含非空行的新 DataTable。这是最好的方法还是有更好的选择?
EDIT: I have also tried iterating over the rows backwards:
编辑:我也试过向后迭代行:
For i = dt.Rows.Count() - 1 To 0
If dt.Rows.Item(i).Item(0).ToString = "" Then
dt.Rows.RemoveAt(i)
End If
Next
回答by Richard Deeming
You can't modify the collection while you're enumerating it with For Each, but a simple Forloop will work. You'll need to loop backwards to avoid skipping the row after a removed row.
使用 枚举集合时不能修改集合For Each,但一个简单的For循环就可以了。您需要向后循环以避免在删除的行之后跳过该行。
You've also got your tests the wrong way round; if Item(0)returns Nothing, then Item(0).ToStringwill throw a NullReferenceException.
你也有错误的测试方法;如果Item(0)返回Nothing,Item(0).ToString则将抛出一个NullReferenceException。
I'm assuming the dt.Rows.Item(0)is a typo, and should read row.Item(0)instead.
我假设这dt.Rows.Item(0)是一个错字,应该改为阅读row.Item(0)。
For i As Integer = dt.Rows.Count - 1 To 0 Step -1
Dim row As DataRow = dt.Rows(i)
If row.Item(0) Is Nothing Then
dt.Rows.Remove(row)
ElseIf row.Item(0).ToString = "" Then
dt.Rows.Remove(row)
End If
Next
回答by Murugan
Vb.Net using linq
使用 linq 的 Vb.Net
Dtset.Tables(0).AsEnumerable().Where(Function(row) row.ItemArray.All(Function(field) field Is Nothing Or field Is DBNull.Value Or field.Equals(""))).ToList().ForEach(Sub(row) row.Delete())
Dtset.Tables(0).AcceptChanges()

