How do I repeat an iteration of a loop?

Fion

Member
Joined
Mar 16, 2006
Messages
9
Programming Experience
Beginner
Hi all, I am attempting to figure out how to repeat an iteration of a loop. I'm hoping that somone knows how to do this.
Basically, I have a datagridview with data in it. I am useing a For Each loop to iterate through each row, and then determine if that row exists in my database. This works perfectly. The issue is that when I determine that a row already exists in the database, I need to delete it from the datagridview. I have been using the datagridview.rows.remove command, but when I do that, the loop skips a line, since it is a for each loop, depending on the rows.
Does anyone know a way around this? I would like to be able to tell the loop to repeat the current row, after the row is deleted (since it will actually be a new row), but I do not know how to tell the loop to do that.
Any ideas?
Thanks!
 
start from the last record in the datagridview and use the for each step -1 to itterate from the last record to the first

this way when one is removed it doesnt skip a row
 
oh fuzz, i read it all wrong

with a For Each loop it wont skip a row anyways, but since it seems to be you could try using a counter and starting from the bottom and working to the top with Step -1
 
The "rule" to not use For Each when removing is common sense because as documentation states, when using the Remove method on an indexed collection the rest of the elements are moved up in the collection and reindexed.

There exist cases when you use a For Each to a collection of elements to remove and actually remove from another collection. Example is when you at different stages of a process add elements to the to-be-removed-collection, then after all processing stages is finished you clean up by iterating the to-be-removed-collection and remove the referenced object from the original collection. This may in some cases result in much cleaner code and workflow than doing the remove logic at every stage of processing a collection where it may be necessary.
 
Back
Top