0

I'm getting an error when I have rows deleted. The way I'm doing it is once a row is deleted, I go back to the previous row and check to make sure and then continues. I'm assuming that the DataTable need to be re-index? I've tried to add AcceptChanges() at the end and it doesn't work too.

Down below is my code:

For i = 0 to dt.Rows.Count - 1
  If dt.Rows(i)("Isbn").StartsWith("B") Then
    dt.Rows(i).Delete()
    i -= 1
  End If
Next
Kluong
  • 302
  • 3
  • 15
  • 1
    `For i = dt.Rows.Count - 1 to 0 Step -1`, remove the `i -= 1` line. – LarsTech Aug 12 '16 at 21:47
  • You need to loop backwards to accomplish what you want. check here for alternate methods: http://stackoverflow.com/questions/1591771/datatable-how-to-conditionally-delete-rows – Steve0 Aug 12 '16 at 21:54
  • Thanks LarsTech, that actually works. Post your answer in the solution and I'll approve it. – Kluong Aug 12 '16 at 22:05
  • Possible duplicate of [Error when deleting rows from DataTable : "There is no row at position X."](http://stackoverflow.com/questions/19927672/error-when-deleting-rows-from-datatable-there-is-no-row-at-position-x) - please do at least a basic search on the error message you receive before posting a new question here. Chances are quite good that you're not the first one to ever see that message. – Ken White Aug 12 '16 at 22:09
  • Don't you think I've searched for it? In addition, do you not see that his code it's a little different from mine? Don't just assume that everybody solution works for everyone. – Kluong Aug 12 '16 at 22:24

0 Answers0