I have a datagridview which places a black border around selected cell(s). My code works fine, up until I scroll while making my selection. My top border disappears when I scroll down while making a selection, which is not a problem at this point, because it shows that there are more selected cells out of view, the problem I have is once I've made my selection and then return to the top, the top border never returns. I've noticed that on enter to a cell, the dgv repaints, and this I assume is the reason why my top border is not painting, because it is out of view and once I scroll back to the top, I'm not entering any new cells, so the top border never repaints.
So I added a scroll event to refresh my dgv paint, but this seems to repaint then scroll and not scroll the repaint, which results in even further selected cell borders not displayed.
So my question is, is there a way to trigger the repaint after the scroll is complete?
Below is my code, not sure if it is relevant to show, but here it is.
Private Sub dgvdefault_paint(sender As Object, e As PaintEventArgs)
With dgvdefault
' draw border around dgv
e.Graphics.DrawRectangle(penborder, 0, 0, .Width - 1, .Height - 1)
For Each cell In .SelectedCells
' get cell position and size
a = cell.rowindex
b = cell.columnindex
Dim myrect As Rectangle = (.GetCellDisplayRectangle(b, a, False))
Dim dgvdefault_headerrectangle As Rectangle = (.GetCellDisplayRectangle(-1, -1, False))
' top border
If a = 0 Then
e.Graphics.DrawLine(Pens.Black, myrect.X - 1, myrect.Y - 1, myrect.X - 1 + myrect.Width, myrect.Y - 1)
ElseIf .Rows(a - 1).Cells(b).Selected = False Then
e.Graphics.DrawLine(Pens.Black, myrect.X - 1, myrect.Y - 1, myrect.X - 1 + myrect.Width, myrect.Y - 1)
End If
' bottom border
If a = .RowCount - 1 Then
e.Graphics.DrawLine(Pens.Black, myrect.X - 1, myrect.Y - 1 + myrect.Height, myrect.X - 1 + myrect.Width, myrect.Y - 1 + myrect.Height)
ElseIf .Rows(a + 1).Cells(b).Selected = False Then
e.Graphics.DrawLine(Pens.Black, myrect.X - 1, myrect.Y - 1 + myrect.Height, myrect.X - 1 + myrect.Width, myrect.Y - 1 + myrect.Height)
End If
' left border
If b = 0 Then
e.Graphics.DrawLine(Pens.Black, myrect.X - 1, myrect.Y - 1, myrect.X - 1, myrect.Y - 1 + myrect.Height)
ElseIf .Rows(a).Cells(b - 1).Selected = False Then
e.Graphics.DrawLine(Pens.Black, myrect.X - 1, myrect.Y - 1, myrect.X - 1, myrect.Y - 1 + myrect.Height)
End If
' right border
If b = .ColumnCount - 1 Then
e.Graphics.DrawLine(Pens.Black, myrect.X - 1 + myrect.Width, myrect.Y - 1, myrect.X - 1 + myrect.Width, myrect.Y - 1 + myrect.Height)
ElseIf .Rows(a).Cells(b + 1).Selected = False Then
e.Graphics.DrawLine(Pens.Black, myrect.X - 1 + myrect.Width, myrect.Y - 1, myrect.X - 1 + myrect.Width, myrect.Y - 1 + myrect.Height)
End If
Next
End With
End Sub
Private Sub dgvdefault_scroll(sender As Object, e As ScrollEventArgs)
dgvdefault.Refresh()
End Sub
Aucun commentaire:
Enregistrer un commentaire