(Windows Forms application using VB.Net)
I have a
DataGridView
with rows displaying the Distance and Estimated Time between two locations called the Route Leg data. I have implemented a
CheckBox
where if
Selected
it should loop through the Route Leg data and
Insert
the Leg Step data below the current Route Leg data.
If the CheckBox is
Not Selected
, a loop will remove all the Leg Step data - this part works well.
The problem is that the DataGridView's RowCount changes every time a Leg Step is added and this causes most of the lower Route Leg detail lines to be skipped.
I have tried For Each, For...Next and While loops to try and loop through the dynamically changing DataGridView. It is as if the start of the loop takes a snapshot of the number of rows (RowCount) at the start of the loop and it keeps to the original number of rows at the start of loop execution.
Here is the code I am using. It is a strip down of the actual code I wish to use:
Private Sub chkShowRouteStep_CheckedChanged(sender As Object, e As EventArgs) Handles chkShowRouteStep.CheckedChanged
Try
If chkShowRouteStep.Checked Then
For i As Integer = 0 To dgvQuote.RowCount - 1
txtRowCount.Text = i
If dgvQuote.Rows(i).Cells(0).Value.ToString <> "" Then
For j As Integer = 1 To 5
dgvQuote.Rows.Insert(i + j, "Step")
Next
End If
i += 1
Next
Else
For i As Integer = dgvQuote.RowCount - 1 To 0 Step -1
If dgvQuote.Rows(i).Cells(0).Value.ToString = "Step" Then
dgvQuote.Rows.RemoveAt(i)
End If
Next
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Is there a way where I can loop through
all the lines, even those added after the loop was started?
What I have tried:
For loop
For each loop
While loop