For Each row As DataGridViewRow In DGV.Rows
DGV.Rows.RemoveAt(CInt(row.Index.ToString))
Next
The above code will remove every other row
For i As Integer = 0 To 7 Step 1
For Each row As DataGridViewRow In DGV.Rows
DGV.Rows.RemoveAt(CInt(row.Index.ToString))
Next
Next
This code gets rid of everything (DGV has, at the time the subroutine is called, 250 rows)
DGV is the DataGridView object which is displayed.
What I don't get is why that first one won't work.
To clarify, I used CInt(row.Index.ToString)
instead of row.Index
because row.Index
does not seem to work on its own, yet CInt(row.Index.ToString)
does.
I have also tried things like:
For i As Integer = 0 To DGV.Rows.Count Step 1
DGV.Rows.RemoveAt(i)
Next
This also failed to work.
I even tried using the actual number 250 in place of DGV.Rows.Count
in the last example, but with the same results, it ALWAYS skips every other. I figured that by iterating through the loop a few more times, I could clear the whole thing, so I used trial and error till I wound up at 7 iterations as the magic number.
Can someone explain this oddity to me?