【发布时间】: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 后立即看到红色。
【问题讨论】:
-
不确定,所以只是评论,但尝试在 datagridview 上使用 Invalidate() 以强制重新绘制。
-
@None 以上令人费解的重复 :)
-
对于那些寻找 C# 示例来处理
DataGridViewCheckBoxColumn的检查更改的人:C# DataGridView Checkbox checked event。
标签: .net vb.net winforms datagridview