Larry's solution will work.
The reason that cycling forward will not work is that the rows are removed from the business object when they are deleted. When you cycle forward through the records and move the CurrentRowIndex forward, it will skip over items like this example:
First Step:
Row1 <- CurrentRowIndex = 0 <- This one is deleted
Row2
Row3
Row4
Row5 <- Count = 5
Second step:
Row2
Row3 <- CurrentRowIndex = 1 <- This one is deleted
Row4
Row5 <- Count = 4
So, on each step, the current row index is advanced, but it skips an item, because all of the items are shifted up at the same time, because you just deleted one of them from the list.
There's basically 3 options:
- Larry's solution of working backwards. The current row index shifts the same direction as the items being deleted.
- Set ShowDeletedRows = True on the business object before you loop through them. This is probably the easiest because you don't have to change your loop logic. All of the records are still visible in the BO after they are deleted, but are flagged as deleted for the save. This is only logical if you're marking the records as deleted to do a bo.Save() at the end.
- Loop while Count > 0 like this:
While bo.Count > 0 '-- or Not bo.IsEmpty
bo.DeleteCurrentRow() '-- Each time you delete a row, the rows will shift up so the next row to delete is always CurrentRowIndex = 0
End While