如何在 vb..net 的 datagridview 中单元格处于编辑模式时绘制边框?

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

这是我在 onpaint 事件中的代码。在我调整表格大小之前工作正常,而不是我遇到不同的故障。伙计,真是个愚蠢的事情,我为此浪费了 3 个小时。任何帮助表示赞赏。

     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

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

这是我使用的,基于 TnTinMnm 的回答,tx。

Private Sub paint_datagrid_edit(sender As Object, e As PaintEventArgs)
    Dim tobjname, objectid
    objectid = get_object_id_by_control(sender.parent.Name)
    tobjname = LCase(objects(objectid).objectref)
    If tobjname = "gridname" Then
        ControlPaint.DrawBorder(e.Graphics, sender.clientrectangle, Color.FromArgb(255, 128, 67), ButtonBorderStyle.Solid)
    End If
End Sub