Click here to Skip to main content
15,905,229 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
(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:

VB
Private Sub chkShowRouteStep_CheckedChanged(sender As Object, e As EventArgs) Handles chkShowRouteStep.CheckedChanged
        Try
            If chkShowRouteStep.Checked Then
                'Show Leg Steps
                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
                'Hide Leg Steps
                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
Posted
Updated 15-Jan-17 1:21am
v3
Comments
Richard MacCutchan 14-Jan-17 5:07am    
You need to adjust your row count as you move through the datagrid for each row added or removed. It is generally a bad idea to do things this way, as it is too easy to miss an added row or skip one when a row is deleted.
Tino Fourie 14-Jan-17 6:52am    
Richard, thanks for the reply. I left out the i += 1 statement in the code block when I pasted the code across. I have editted the post to include the For loop stepping.

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900