Active Cell not highlighting with CellPainting

Viewed 57

(First time posting, please be gentle.)

I have a DataGridView on my form, that makes the cells in the current row and column appear highlighted. I also added a CellPainting part to give the active cell a thicker border. The code goes like this:

Private Sub DGV_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles DGV.CellPainting
        Try
            Dim CRow As Integer = DGV.CurrentCell.RowIndex
            Dim CCol As Integer = DGV.CurrentCell.ColumnIndex
            If e.RowIndex = CRow AndAlso e.ColumnIndex = CCol Then
                Using BBrush As Brush = New SolidBrush(Color.Black)
                    Using BPen As Pen = New Pen(BBrush, 2)
                        Dim CBRect As Rectangle = e.CellBounds
                        CBRect.Width -= 1
                        CBRect.Height -= 1
                        CBRect.X = rectDimensions.Left
                        CBRect.Y = rectDimensions.Top
                        e.Graphics.DrawRectangle(BPen, CBRect)
                        e.Handled = True
                    End Using
                End Using
            End If
        Catch
        End Try
    End Sub

The above works fine in the sense it adds the border to the cell, however the active cell does not show up as highlighted, just white. I do not understand as there is no specification of FillRectangle() or of any other colours than the black border, so I assumed it would keep the cell colour the same, but it does not go blue like the other cells in its row and column.

Any help would be greatly appreciated :)

1 Answers

The line of your code e.Handled = True tells VB.NET not to continue with any future event handling, thus, the cell is not painted blue. If you remove that line, it should work fine.

Edit: Your border is not applied completely because in your code, you use the Graphics object provided by the event, which represents the graphics of the cell, but not of the entire DataGridView. Thus, you cannot draw outside of the cell with the graphics object obtained by the event. And the border you were trying to draw happens to be partially outside of the cell. So here is a sample which should work as intended:

Private Sub DataGridView1_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
    If DataGridView1.CurrentCell Is Nothing Then Return
    Dim graphics As Graphics = DataGridView1.CreateGraphics()
    Dim CRow As Integer = DataGridView1.CurrentCell.RowIndex
    Dim CCol As Integer = DataGridView1.CurrentCell.ColumnIndex
    If e.RowIndex = CRow AndAlso e.ColumnIndex = CCol Then
        Using BBrush As Brush = New SolidBrush(Color.Black)
            Using BPen As Pen = New Pen(BBrush, 3)
                Dim CBRect As Rectangle = e.CellBounds
                CBRect.Width -= 1
                CBRect.Height -= 1
                graphics.DrawRectangle(BPen, CBRect)
            End Using
        End Using
    End If
    graphics.Dispose()
End Sub

Here, we use the CreateGraphics() method to obtain a Graphics object which represents the entire DataGridView. And, just another piece of advice, use logical statements instead of Try/Catch if possible, as I've done in the above code (first line). In addition, you should not leave the Catch block empty, as it makes it difficult to solve runtime errors. Hope this helps!

Related