I faced a problem where I wanted to Show / Hide detailed information dynamically in a
I am developing a system which incorporates Routing information. In a section of the application, I wanted to provide the user an option between a General View and a Detailed View of the Routing information.
The General View would only show Route Leg information which is the Origin and the Destination, total distance and total Estimated Duration whereas the Detailed View will display the Steps for each Leg of the Route.
Using the Code
Originally, I focused on the
Rows.Count and also
RowCount to try and loop through the rows in the
The problem was that at loop execution, a "snapshot" of the row count was taken. This means that every time I
Insert new rows, the newly added rows will not be taken into account and it led to many different loop options which all failed -
For...Next and also
The problem was the "snapshot" and because I had no certainty that it was actually the problem, I took a different approach by not looping on the number of rows but instead of setting a trigger (
False) when I have re-evaluated the number of rows to the current loop count (
i += 1).
The result was a perfect solution to the problem.
Below is the code implementation I am now using to achieve the goal of Show / Hide details dynamically.
Private Sub chkShowRouteStep_CheckedChanged(sender As Object, e As EventArgs) _
Dim lastRow As Boolean = False
Dim i As Integer = 0
If chkShowRouteStep.Checked Then
While lastRow <> True
If dgvQuote.Rows(i).Cells(0).Value.ToString <> "" Then
For j As Integer = 1 To 2
dgvQuote.Rows.Insert(i + j, "", "", "Step")
If i = dgvQuote.Rows.Count - 1 Then
lastRow = True
i += 1
For x As Integer = dgvQuote.RowCount - 1 To 0 Step -1
If dgvQuote.Rows(x).Cells(2).Value.ToString = "Step" Then
Catch ex As Exception
- 15th January, 2017: Initial version