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

Vb.net 在vb..net的datagridview中,当单元格处于编辑模式时,如何绘制边框?,vb.net,datagridview,border,cell,Vb.net,Datagridview,Border,Cell,这是我在onpaint事件中的代码。工作正常,直到我调整表单大小,然后我得到不同的小故障。老兄,这么愚蠢的事,我在上面浪费了3个小时。谢谢你的帮助 Using backGroundPen = New Pen(e.CellStyle.BackColor, 1) Using gridlinePen = New Pen(Color.Black, 1) Using selectedPen = New Pen(Color.FromArgb(

这是我在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.EditingPanel
订阅其
Paint
事件来为其绘制自定义边框是很简单的


这是我所用的,基于德克萨斯州TnTinMnm的答案

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

. 仅应用于进入编辑模式的单元格。不要使用
DataGridView.Paint
事件:CellPaint、RowPrePaint或RowPostPaint或是您需要的事件。EditingPanel@JQSOFT,我不知道您是如何获得NRE的,因为设计为不返回空引用。您是不是碰巧使用了编辑控件?这就是原因。我再次道歉++
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