背景色不为整个单元格仅着色文本

Backcolor not Coloring Entire Cell only Text

对于 Internet,我提取并修改了一些代码,以在编辑文本时更改 datagridview 中当前单元格的背景色。问题是,背景色似乎只在单元格文本内发生变化,而不是整个单元格(在具有原始颜色的单元格中似乎有 3 个像素的边距)。下面是我正在使用的代码:

Public Class Form1

Dim a, b As Integer
Dim myvalue As String

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    a = 0
    Do While a < 10
        Me.DataGridView1.Rows.Add()
        a += 1
    Loop
End Sub

Private EditingControl As DataGridViewTextBoxEditingControl

Private Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
    EditingControl = e.Control
    AddHandler EditingControl.TextChanged, AddressOf EditingControl_TextChanged
End Sub

Private Sub EditingControl_TextChanged(sender As Object, e As EventArgs)
    EditingControl.BackColor = Color.White
End Sub

Private Sub DataGridView1_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
    If EditingControl IsNot Nothing Then
        RemoveHandler EditingControl.TextChanged, AddressOf EditingControl_TextChanged
    End If
    EditingControl = Nothing
End Sub
End Class

有人知道我在代码中做错了什么吗?

EditingControl 放在面板上,有 3 个像素的边距。

为父面板着色可以解决您的问题:

Private Sub EditingControl_TextChanged(sender As Object, e As EventArgs)
    EditingControl.BackColor = Color.White
    EditingControl.Parent.BackColor = Color.White
End Sub

顺便说一下,DataGridView class 已经有一个 EditingControl 属性。