Backcolor不着色整个单元格文本

时间:2015-03-24 15:14:54

标签: vb.net datagridview colors cell textchanged

对于互联网,我提取并修改了一些代码,以便在编辑文本时更改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

有人知道我对代码做错了吗?

1 个答案:

答案 0 :(得分:1)

EditingControl放在面板上,边距为3像素。

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

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

顺便说一句,DataGridView类已经有EditingControl属性。