【问题标题】:Change BackColor of DataGridViewCheckBoxColumn on Check or Uncheck在选中或取消选中时更改 DataGridViewCheckBoxColumn 的背景颜色
【发布时间】:2019-01-29 00:49:20
【问题描述】:

1- 在Form1上添加一个DataGridView,命名为DataGridView1。

2- 将以下代码复制并粘贴到后面的代码中。

Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    Dim myColumn As New DataGridViewCheckBoxColumn With {.ValueType = GetType(Boolean), .Name = "Option", .HeaderText = "Option"}
    myColumn.DefaultCellStyle.SelectionBackColor = System.Drawing.Color.Transparent
    DataGridView1.Columns.Add(myColumn)
    For ii As Integer = 1 To 2
        DataGridView1.Rows.Add({True})
    Next
End Sub
Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
    If DataGridView1.Columns(e.ColumnIndex).Name = "Option" AndAlso DataGridView1.Rows(e.RowIndex).IsNewRow = False Then
        If e.Value = False Then
            e.CellStyle.BackColor = System.Drawing.Color.Red
            'I tried following codes but they are not
            'DataGridView1.Refresh()
            'DataGridView1.Update()
        End If
    End If
End Sub
End Class

3- 运行此项目并取消选中其中一个复选框。

我希望在单击 CheckBox 后立即看到红色。

【问题讨论】:

标签: .net vb.net winforms datagridview


【解决方案1】:

考虑这些点以实现检查时更改单元格的背景颜色,并删除选择突出显示:

  1. 要将复选框值推送到单元格,您需要处理CellContentClickCellContentDoubleClick 并使用DataGridView.CommitEdit(DataGridViewDataErrorContexts) 方法提交对单元格值的更改。如果不这样做,在您结束编辑之前,该值不会推送到单元格。

  2. 要使选择的背景颜色不可见,您需要将单元格的SelectionBackColor设置为与BackColor相同的颜色。

那么你可以有这样的东西:

在示例中,我将False 显示为RedTrueDbNull.Value 显示为While。您可以根据需要更改逻辑。要实现您在动画中看到的效果,请将DataGridView 拖放到表单上并粘贴以下代码:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim table = New DataTable()
    table.Columns.Add("C1", GetType(Boolean))
    table.Columns.Add("C2", GetType(String))
    table.Rows.Add(True, "A")
    table.Rows.Add(False, "B")
    DataGridView1.DataSource = table
End Sub
Private Sub DataGridView1_CellContentClick(sender As Object,
    e As DataGridViewCellEventArgs) _
    Handles DataGridView1.CellContentClick, DataGridView1.CellContentDoubleClick

    If e.ColumnIndex = 0 AndAlso e.RowIndex >= 0 Then
        DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
        DataGridView1.InvalidateCell(e.ColumnIndex, e.RowIndex)
    End If
End Sub
Private Sub DataGridView1_CellPainting(sender As Object,
    e As DataGridViewCellPaintingEventArgs) _
    Handles DataGridView1.CellPainting

    If e.ColumnIndex = 0 AndAlso e.RowIndex >= 0 Then
        If TypeOf (e.Value) Is Boolean AndAlso e.Value = False Then
            e.CellStyle.BackColor = Color.Red
        Else
            e.CellStyle.BackColor = Color.White
        End If
    End If
    e.CellStyle.SelectionBackColor = e.CellStyle.BackColor
    e.CellStyle.SelectionForeColor = e.CellStyle.ForeColor
End Sub

注意

您也可以使用此处描述的解决方案:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-19
    • 2018-03-04
    • 2022-12-18
    • 2021-01-12
    • 1970-01-01
    • 2019-03-22
    相关资源
    最近更新 更多