Tino Fourie
Tino Fourie

Reputation: 59

How do I loop through a DataGridView where RowCount / Rows.Count changes

I have a VB.Net Form with a DataGridView where I wish to change between a Detailed View and a General View.

The DataGridView shows the Distance and Estimated Time between places - called the Route Leg Data and is termed the General View

I have implemented a CheckBox to select between the general and detailed view. When the CheckBox is Checked, I am trying to loop through all of the Route Leg entries and insert the Route Steps which is the Detailed information about the Leg entries.

I have tried various loop options: For..Next, For Each...Next, While...End While, and only the first row (Route Leg) gets processed, even if I have 5 more Route Leg entries.

Important: Keep in mind that when the Detail View is selected, the DataGridView row count increments for every new Route Step entry that gets inserted.

I tried to use both dgv.RowCount and dgv.Rows.Count but I keep getting the same result.

I have added some code to show what I am trying to achieve. Any help or guidance will be much appreciated.

'Show / Hide Route Step Data
Private Sub chkShowRouteStep_CheckedChanged(sender As Object, e As EventArgs) Handles chkShowRouteStep.CheckedChanged
    Try
        If chkShowRouteStep.Checked Then
            'Show Route 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
                        i += 1
                        dgvQuote.Rows.Insert(i, "Step")
                        'dgvQuote.Rows.Insert(j + i, "Step")

                    Next
                End If
            Next

        Else
            'Hide Route Steps - WORKS GREAT
            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

Upvotes: 0

Views: 2344

Answers (2)

Tino Fourie
Tino Fourie

Reputation: 59

My impatience, as well as a refocus, provided me with the answer.

My focus was always on the DataGridView and the feedback from it with regards to the number of rows contained in the control.

However, the execution of a loop took a snapshot of the number of rows at the start of the loop execution and did NOT update the number of rows every time rows were added to the DataGridView.

I took a different approach where I compared my current loop number (i += 1) with the current number of rows in the DataGridView (dgv.Rows.Count - 1) thereby forcing a recheck on the number of rows. This means that any new rows added to the DataGridView will now be counted in.

I added a trigger (True/False) to be set to true if the last row in the DataGridView was reach and the While...End While loop was exited.

'Show / Hide Route Step Data
Private Sub chkShowRouteStep_CheckedChanged(sender As Object, e As EventArgs) Handles chkShowRouteStep.CheckedChanged
    Try
        Dim lastRow As Boolean = False 'This will get set when we have reached the last row in the DGV
        Dim i As Integer = 0

        If chkShowRouteStep.Checked Then
            'Show Route Steps
            While lastRow <> True

                txtRowCount.Text = i

                If dgvQuote.Rows(i).Cells(0).Value.ToString <> "" Then

                    For j As Integer = 1 To 2
                        'i += 1
                        dgvQuote.Rows.Insert(i + j, "", "Step")

                    Next

                End If
                'Check to see if we have reached the last row, set lastRow to TRUE if it is the last row

                If i = dgvQuote.Rows.Count - 1 Then
                    lastRow = True
                End If

                i += 1

            End While

        Else
            'Hide Route Steps - WORKS GREAT
            For x As Integer = dgvQuote.RowCount - 1 To 0 Step -1
                If dgvQuote.Rows(x).Cells(1).Value.ToString = "Step" Then
                    dgvQuote.Rows.RemoveAt(x)
                End If
            Next
        End If
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

The result is as follows and error free:

Before enter image description here

After enter image description here

Upvotes: 1

Charles May
Charles May

Reputation: 1763

Since the comment in the code says you want to hide route steps, why not do just that? Instead of removing and inserting rows, populate the grid with everything and then use the checkbox to set the rows .Visible property?

Upvotes: 0

Related Questions