Search code examples
vb.netdatagridviewbordercell

How to draw a border when cell is in edit mode in datagridview in vb..net?


Here is my code from onpaint event. Works OK until I resize the form, than I get different glitches. Man such a stupid thing and I lost like 3 hours on it. Any help appreciated.

     Using backGroundPen = New Pen(e.CellStyle.BackColor, 1)
            Using gridlinePen = New Pen(Color.Black, 1)
                Using selectedPen = New Pen(Color.FromArgb(254, 169, 62), 1)
                    'Using selectedPen = New Pen(Color.Red, 1)
                    Dim topLeftPoint = New Point(e.CellBounds.Left - 1, e.CellBounds.Top)
                    Dim topRightPoint = New Point(e.CellBounds.Right - 1, e.CellBounds.Top)
                    Dim bottomRightPoint = New Point(e.CellBounds.Right - 1, e.CellBounds.Bottom - 1)
                    Dim bottomleftPoint = New Point(e.CellBounds.Left - 1, e.CellBounds.Bottom - 1)
                    If e.ColumnIndex > -1 And e.RowIndex > -1 AndAlso sender.Rows(e.RowIndex).Cells(e.ColumnIndex).IsInEditMode Then
                        e.Paint(e.ClipBounds, DataGridViewPaintParts.All And Not DataGridViewPaintParts.Border)
                        e.Graphics.DrawRectangle(selectedPen, New Rectangle(e.CellBounds.Left, e.CellBounds.Top, e.CellBounds.Width - 1, e.CellBounds.Height - 1))
                        e.Handled = True
                    Else
                        e.Paint(e.ClipBounds, DataGridViewPaintParts.All And Not DataGridViewPaintParts.Border)
                        If e.RowIndex = -1 Then e.Graphics.DrawLine(backGroundPen, topLeftPoint, topRightPoint)
                        If e.ColumnIndex = -1 Then e.Graphics.DrawLine(backGroundPen, topLeftPoint, bottomleftPoint)

                        If e.RowIndex = sender.RowCount - 1 Then
                            e.Graphics.DrawLine(gridlinePen, bottomRightPoint, bottomleftPoint)
                        Else
                            e.Graphics.DrawLine(backGroundPen, bottomRightPoint, bottomleftPoint)
                        End If

                        If e.ColumnIndex = sender.ColumnCount - 1 Then
                            e.Graphics.DrawLine(gridlinePen, bottomRightPoint, topRightPoint)
                        Else
                            e.Graphics.DrawLine(backGroundPen, bottomRightPoint, topRightPoint)
                        End If

                        If e.RowIndex > 0 Then e.Graphics.DrawLine(gridlinePen, topLeftPoint, topRightPoint)
                        If e.ColumnIndex > 0 Then e.Graphics.DrawLine(gridlinePen, topLeftPoint, bottomleftPoint)
                        e.Handled = True
                    End If
                End Using
            End Using
        End Using

Solution

  • The DataGridView.EditingControl is parented to the DataGridView.EditingPanel. Knowing that, it is simple to draw a custom border for the DataGridView.EditingPanel by subscribing to its Paint event by calling ControlPaint.DrawBorder.

    Protected Overrides Sub OnLoad(e As EventArgs)
      MyBase.OnLoad(e)
      AddHandler DataGridView1.EditingPanel.Paint, Sub(s As Object, pe As PaintEventArgs) ControlPaint.DrawBorder(pe.Graphics, New Rectangle(Point.Empty, DataGridView1.EditingPanel.Size), Color.FromArgb(254, 169, 62), ButtonBorderStyle.Solid)
    End Sub