I forgot one minor detail...tell the loop to count backwards:
Dim i as Integer
For i = Me.HidWkFlwDataSet.tblPod.Rows.Count - 1 to 0 Step -1
If Me.HidWkFlwDataSet.tblPod.Rows(i).Verified = False Then
Me.HidWkFlwDataSet.tblPod.Rows.RemoveAt(i)
End If
Next i
I think that should fix your problem.
FYI about the other exception:
You’re receiving an IndexOutOfRangeException because you’re removing items from the collection while iterating forward. For example, when I delete the row at index 2, all rows following index 2 have their index change to n – 1 ( row 3 becomes row 2, row 4 becomes row 3, etc.). The for loop starts with the original count, but now the count is decreasing and your indices are changing. When you get to the end of the collection you’re trying to access an invalid index. Iterating backwards preserves your indices.
Also, I think you can use your first solution (foreach) if you use row.Delete(). That modifies the row's state. After the loop you call Me.HidWkFlwDataSet.tblPod.AcceptChanges() and the data table will remove all rows marked for deletion.
Hope this helps.
|