When deleting from collections by index, don't start at the front and work through to the end.
Start at the end and work backwards to the front.
Imagine you have 4 rows of data
Row # Data
1 Data Row 1
2 Data Row 2
3 Data Row 3
4 Data Row 4
Say you want to delete rows number 4 and 1. Start at the top and delete the first row and see what happens
Row # Data
1 Data Row 2
2 Data Row 3
3 Data Row 4
I.e. what was row number 2 is now row number 1 ... and there is no longer a row number 4
If you do it the other way around ... start at the end and delete row number 4
Row # Data
1 Data Row 1
2 Data Row 2
3 Data Row 3
Rows 1,2 and 3 "stay" where they were and can still be referenced by their original row numbers
EDIT after OP comment
Instead of using a loop counter that looks like this
for (int j = 0; j < row1.Count; j++)
you would use a loop that looks more like this (warning - untested code)
for (int j = row1.Count - 1; j >=0; j--)
Things to note:
1. The "start" of the loop is the number of rows minus 1 - because the row counter starts at zero.
2. The loop termination (
j >= 0
must include 0
3. instead of counting j upwards (
j++
) you decrement j (count down) with
j==