I cant able to remove a row from a table while running through a loop
I am iterating through the table rows . if the row row is null I just want to remove the row. it shows an error
Public Sub RemoveBlankRow(ByVal MyDataTable As DataTable)
Try
Dim MyRowStr As String = String.Empty
For Each MyRow As Da开发者_高级运维taRow In MyDataTable.Rows
MyRowStr = ""
For Each MyCellText As Object In MyRow.ItemArray
MyRowStr = MyRowStr + MyCellText.ToString
Next
MyRowStr = Replace(MyRowStr, "0", " ")
MyRowStr = Replace(MyRowStr, ".", " ")
If MyRowStr.Trim = String.Empty Then
MyDataTable.Rows.Remove(MyRow)
End If
Next
Catch ex As Exception
End Try
End Sub
How to overcome this issue?
In general you can't modify the structure of a collection while you iterate over it.
A simple solution is while iterating over your table, store the rows that need to be deleted in a separate deletion list. When you have finished iterating, iterate over your list of deletions and delete them from the original list.
Create a copy of your MyDataTable.Rows
, iterate over that and remove the rows from your original collection.
精彩评论