Datagridview 中的方形填充复选框

Square Filled CheckBox in Datagridview

我有一个看起来像这样的数据库

Table 姓名:ri_closure

正如您在图片上方看到的,除了 Month 之外的所有列都是 TinyInt(1) 并且 MonthVarchar 现在我这里有一个代码

 Dim con1 As MySqlConnection = New MySqlConnection("server=192.168.2.87;userid=root;password=admin1950;database=inventory")
        Dim sql1 As MySqlCommand = New MySqlCommand("Select * from ri_closure", con1)
        Dim ds1 As DataSet = New DataSet
        Dim adapter1 As MySqlDataAdapter = New MySqlDataAdapter
        con1.Open()
        adapter1.SelectCommand = sql1
        adapter1.Fill(ds1, "MyTable")
        DataGridView2.DataSource = ds1.Tables(0)
        con1.Close()
        ds1.Tables(0).Columns(2).DataType = GetType(Boolean)
        Me.DataGridView2.Columns(1).Frozen = True
        Dim i As Integer
        For i = 0 To DataGridView2.Columns.Count - 1
            DataGridView2.Columns.Item(i).SortMode = DataGridViewColumnSortMode.Programmatic
        Next
        DataGridView2.Columns(0).Visible = False
        DataGridView2.Columns(1).DefaultCellStyle.BackColor = Color.LightBlue

现在是这个样子

现在你看到了输出,我相信你不想要那个红色的。刺激眼睛,这是我的代码

  For Each rw As DataGridViewRow In DataGridView2.Rows
            For ii As Integer = 2 To rw.Cells.Count - 1
                If rw.Cells(ii).Value = False Then
                    rw.Cells(ii).Style.BackColor = Color.Red
                ElseIf rw.Cells(ii).Value = True Then
                    rw.Cells(ii).Style.BackColor = Color.White
                End If
            Next
        Next

现在这是我的问题,我希望这是可能的。我想做这样的事情而不是 uncked 并将单元格变成红色我怎样才能像这样制作 uncked 单元格。

它看起来与上图相同,而不是空复选框。

我希望另一个将生成填充的复选框的值将替换未检查的值,因为我有代码检查那个。

我尝试了一些代码,这是输出

TYSM 未来的帮助

您可以利用 DataGridViewVirtualMode,然后使用 CellValueNeeded 事件来显示您自己的内容。

这是我建议你应该做的,

  1. 决定要显示的图像而不是复选框(例如绿色勾号)
  2. DataGridViewVirtualMode 属性 设置为 true
  3. DataGridView 与您的 DataTable 绑定后遍历所有月份列,将 DataProperptyName 设置为空字符串(这对于触发事件很重要)
  4. DataGridViewCellValueNeeded 事件创建事件处理程序,在此事件中,您将在事件参数中接收列索引和行索引。使用它来查找后面的实际值,然后 return 绿色勾号图像(使用 e.Value)或 return 空白图像,具体取决于值。

您可以通过处理 CellPainting event of DataGridView. Then you can use CheckBoxRenderer to draw the check box in desired state. The state which you want to show for unchecked state of check box is CheckBoxState.MixedNormal:

自定义 DataGridViewCheckBox 列的绘制
Private Sub CellPainting(ByVal sender As Object, _
    ByVal e As DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
    If (e.ColumnIndex = 0 AndAlso e.RowIndex >= 0) Then
        Dim value = DirectCast(e.FormattedValue, Nullable(Of Boolean))
        e.Paint(e.CellBounds, DataGridViewPaintParts.All And _
                              Not (DataGridViewPaintParts.ContentForeground))
        Dim state = IIf((value.HasValue And value.Value), _
                        VisualStyles.CheckBoxState.CheckedNormal, _
                        VisualStyles.CheckBoxState.MixedNormal)
        Dim size = RadioButtonRenderer.GetGlyphSize(e.Graphics, state)
        Dim location = New Point((e.CellBounds.Width - size.Width) / 2, _
                                (e.CellBounds.Height - size.Height) / 2)
        location.Offset(e.CellBounds.Location)
        CheckBoxRenderer.DrawCheckBox(e.Graphics, location, state)
        e.Handled = True
    End If
End Sub

要测试解决方案,您可以通过这种方式向网格添加一列:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles MyBase.Load
    Dim C1 = New DataGridViewCheckBoxColumn()
    C1.DataPropertyName = "C1"
    C1.HeaderText = "C1"
    C1.TrueValue = 1
    C1.FalseValue = 0
    Me.DataGridView1.Columns.Add(C1)
    Me.DataGridView1.Rows.Add(DirectCast(1, Object))
    Me.DataGridView1.Rows.Add(DirectCast(0, Object))
    Me.DataGridView1.AllowUserToAddRows = False
End Sub

这将是结果:

要在调用 CheckBoxRenderer.DrawCheckBox 后用红色绘制未选中(实际上是混合状态),请使用此代码:

If (state = VisualStyles.CheckBoxState.MixedNormal) Then
    Dim rect = New Rectangle(location, size)
    rect.Inflate(-2, -2)
    e.Graphics.FillRectangle(Brushes.Red, rect)
End If