FAQ Item: Why does the DataGridView not show up the RowHeader on the second tab of a TabControl?

Locked FAQ Item: Why does the DataGridView not show up the RowHeader on the second tab of a TabControl?

Locked

  • Sunday, June 20, 2010 12:44 PM
     
     
    Why does the DataGridView not show up the RowHeader on the second tab of a TabControl?

All Replies

  • Sunday, June 20, 2010 12:44 PM
     
     Answered

    The cause of this issue is that the DataGridView is not updated while it is hidden. We can use the TabControl’s SelectedIndexChanged event to work around the problem. Every time a tab is selected, this event occurs, and then we call DataGridView.Invalidate() to force the DataGridView to be redrawn.

    Codes:

        Public Sub Tab_Changes(ByVal sender As System.Object, ByVal e As System.EventArgs)

            ' use this sub to track if a tab is selected in the tab control & force a row and column redraw

            Dim tabC As TabControl

            Dim dgvChart As DataGridView

            Dim strTab As String

     

            'cast to TabControl to work with

            If TypeOf sender Is TabControl Then

                tabC = DirectCast(sender, TabControl)

     

                'cast to DGV to work with

                If TypeOf tabC.TabPages(tabC.SelectedIndex).Controls(dgvChart.Name) Is DataGridView Then

                    dgvChart = DirectCast(tabC.TabPages(tabC.SelectedIndex).Controls(DataGridView1.Name), DataGridView)

     

                    'Invalidate the tabpage's dgv

                    dgvChart.Invalidate()

     

                    ' OR call a bespoke sub called Sub_DGVRedraw(Byval dgvInput as DataGridView) that will redraw the headers

                    Sub_DGVRedraw(dgvChart)

                End If

            End If

        End Sub

     

    Related threads:

    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/00c461e7-7a83-43da-b60e-fb192463166d

    http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/b4f5079c-c8f1-4a2d-a476-8cbef1e0debb

    http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/44e13cb2-15e2-4b40-b8ed-9adb80e9b6c7/

     

    For more FAQ about Visual Basic .NET General, please see Visual Basic .NET General FAQ

    • Marked As Answer by MSDN FAQ Sunday, June 20, 2010 12:44 PM
    •