Click here to Skip to main content
14,237,256 members

How to Create Show / Hide Details Dynamically in a DataGridView

Rate this:
3.56 (4 votes)
Please Sign up or sign in to vote.
3.56 (4 votes)
15 Jan 2017CPOL
Catering for user needs in a dynamic manner using a DataGridView

Introduction

I faced a problem where I wanted to Show / Hide detailed information dynamically in a DataGridView.

Background

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.

General View

Before.jpg

Detailed View

After.jpg

Using the Code

Originally, I focused on the DataGridView's .Rows.Count and also .RowCount to try and loop through the rows in the DataGridView.

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 Each...Next, For...Next and also While...End While.

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 (True/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.

'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

                   If dgvQuote.Rows(i).Cells(0).Value.ToString <> "" Then
                       'Write Step Data
                       For j As Integer = 1 To 2
                           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
                   'Increment loop count
                   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(2).Value.ToString = "Step" Then
                       dgvQuote.Rows.RemoveAt(x)
                   End If
               Next
           End If
       Catch ex As Exception
           MsgBox(ex.Message)
       End Try
   End Sub

History

  • 15th January, 2017: Initial version

License

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

Share

About the Author

Tino Fourie
South Africa South Africa
Studied Software Development many moons ago, when the sun was still high for Cobol, Fortran, Basic, etc. programmers. However, I found corporate networks and systems more challenging and rewarding than sitting behind a desk all day long doing mostly maintenance on code. Mainframes were the super computers at the time and Tatung made very fancy desktop computers and to soop them up you had to have a TsengLabs VGA card, EGA CRT and a SoundBlaster sound card installed into the system - only then could you call yourself a Boss!

Later in life I reached a turning point where the saying "Jack of all trades, Master of none" became my driving force. I will do everything I know nothing about. Since then, life has become an adventure and not just a journey.

At the moment I write software tools to connect users with disconnected data.

Comments and Discussions

 
QuestionNot an article Pin
Richard MacCutchan15-Jan-17 1:58
protectorRichard MacCutchan15-Jan-17 1:58 
AnswerRe: Not an article Pin
Tino Fourie15-Jan-17 2:21
memberTino Fourie15-Jan-17 2:21 
GeneralRe: Not an article Pin
Richard MacCutchan15-Jan-17 2:45
protectorRichard MacCutchan15-Jan-17 2:45 
GeneralRe: Not an article Pin
Tino Fourie15-Jan-17 2:59
memberTino Fourie15-Jan-17 2:59 
GeneralRe: Not an article Pin
Richard MacCutchan15-Jan-17 3:02
protectorRichard MacCutchan15-Jan-17 3:02 
GeneralRe: Not an article Pin
Tino Fourie15-Jan-17 3:15
memberTino Fourie15-Jan-17 3:15 
GeneralRe: Not an article Pin
Richard MacCutchan15-Jan-17 3:24
protectorRichard MacCutchan15-Jan-17 3:24 
AnswerRe: Not an article Pin
OriginalGriff15-Jan-17 3:57
protectorOriginalGriff15-Jan-17 3:57 
GeneralRe: Not an article Pin
Richard MacCutchan15-Jan-17 4:11
protectorRichard MacCutchan15-Jan-17 4:11 
GeneralRe: Not an article Pin
OriginalGriff15-Jan-17 4:15
protectorOriginalGriff15-Jan-17 4:15 
GeneralRe: Not an article Pin
Richard MacCutchan15-Jan-17 4:34
protectorRichard MacCutchan15-Jan-17 4:34 
AnswerRe: Not an article Pin
Jeffamn16-Jan-17 7:22
memberJeffamn16-Jan-17 7:22 
GeneralRe: Not an article Pin
Richard MacCutchan16-Jan-17 22:15
protectorRichard MacCutchan16-Jan-17 22:15 
GeneralRe: Not an article Pin
Tino Fourie17-Jan-17 6:08
memberTino Fourie17-Jan-17 6:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Tip/Trick
Posted 15 Jan 2017

Tagged as

Stats

7.1K views
4 bookmarked