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 PMWhy does the DataGridView not show up the RowHeader on the second tab of a TabControl?
All Replies
-
Sunday, June 20, 2010 12:44 PM
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
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

