On my main form, I have a MenuStrip with 16 items. When the user drops one of the items down, and then scrolls left to right without actually clicking on an item, my DataGridView underneath does not get redrawn (the DGV has Paint overriden) and therefore still shows some of the now hidden menu item.
I have tried using the MenuDeactivate event, but this does not seem to fire when the user scrolls sideways on the items.
Option Strict On
Option Explicit On
Public Class Form1
Private WithEvents dgvTest As DataGridView
Private WithEvents mnuRefresh As ToolStripMenuItem
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.WindowState = FormWindowState.Maximized
Dim mnuMain As New MenuStrip
For i As Integer = 10 To 90 Step 10
Dim T As New ToolStripMenuItem
T.Text = String.Format("Menu {0:00}", i)
mnuMain.Items.Add(T)
For j As Integer = 1 To 9
T.DropDownItems.Add(String.Format("Menu {0:00}", i + j))
Next
Next
mnuRefresh = New ToolStripMenuItem
mnuRefresh.Text = "Refresh"
mnuMain.Items.Add(mnuRefresh)
Me.Controls.Add(mnuMain)
'Add DataGridView
dgvTest = New DataGridView
dgvTest.Dock = DockStyle.Fill
Me.Controls.Add(dgvTest)
'add test columns
For i As Integer = 1 To 10
Dim C As New DataGridViewTextBoxColumn
C.Name = String.Format("Column {0:00}", i)
dgvTest.Columns.Add(C)
Next
'add test rows
dgvTest.Rows.Add(20)
End Sub
Private Sub dgvTest_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles dgvTest.CellPainting
If e.RowIndex < 0 OrElse e.ColumnIndex < 0 Then
e.Handled = False
Exit Sub
End If
If e.RowIndex = 5 AndAlso e.ColumnIndex = 0 Then
Dim WidthToUse As Integer = e.CellBounds.Width
For j As Integer = 1 To dgvTest.Columns.Count - 1
If dgvTest.Columns(j).Visible = True Then
WidthToUse += dgvTest.Rows(e.RowIndex).Cells(j).Size.Width
End If
Next
With e.Graphics
Using backColorBrush As New SolidBrush(Color.Black)
Using gridLinePen As New Pen(dgvTest.GridColor)
.FillRectangle(backColorBrush, e.CellBounds.Left, e.CellBounds.Top, WidthToUse, e.CellBounds.Height)
If e.RowIndex < dgvTest.Rows.Count - 1 Then
'draw bottom line of current cell
.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1)
End If
' Draw right border line of furthest cell
.DrawLine(gridLinePen, e.CellBounds.Left + WidthToUse - 1, e.CellBounds.Top, e.CellBounds.Left + WidthToUse - 1, e.CellBounds.Bottom)
.DrawString(String.Format("HEADER ROW ", e.RowIndex), New Font("Arial", 11, FontStyle.Bold), Brushes.White, e.CellBounds.X + 2, e.CellBounds.Y + 5, StringFormat.GenericDefault)
End Using
End Using
End With
e.Handled = True
ElseIf e.RowIndex = 5 AndAlso e.ColumnIndex > 0 Then
'REMAINDER OF GROUP ROW, do not paint at all
e.Handled = True
Else
'normal row
End If
End Sub
Private Sub mnuRefresh_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuRefresh.Click
dgvTest.Refresh()
End Sub
End Class
I tried that code, clicked a menu items and 'scrolled' back and forth the other dropdown items, and I'm still not seeing any problems with the menus and DGV graphics.
One problem I did see was when resizing form the header row behaves badly, since it only redraws when column 0 needs to redraw. The Paint event could be used for header rows instead, or perhaps call InvalidateRow for header rows on form Resize.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.