数据网格视图中填充方形复选框

3

我有一个数据库长这样:

表名:ri_closure enter image description here

如上图所示,除了Month列之外,所有列都是TinyInt(1)类型,而Month列是Varchar类型。现在我有一段代码:

 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

现在它看起来是这样的:

enter image description here

现在您可以看到输出,我相信您不想要那些红色的。它刺激眼睛。这是我用于解决此问题的代码。
  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

现在我有一个问题,希望您能帮忙回答。我想做的是将未选中的单元格变成红色,而不是保持默认状态。请问如何实现这一点?

enter image description here

不再显示空复选框,而是与上面的图像相同。

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

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

enter image description here

感谢你未来的帮助。
2个回答

2
您可以通过处理 DataGridViewCellPainting 事件来自定义 DataGridViewCheckBox 列的绘制。然后,您可以使用 CheckBoxRenderer 来绘制所需状态的复选框。对于未选中状态的复选框,您想要显示的状态是 CheckBoxState.MixedNormal
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

这是结果:

这将是结果:

enter image description here

为了在调用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

enter image description here


先生,您能否在我的代码中的表单加载时加入您的代码?谢谢。 - user6825121
这与表单加载无关。处理网格的CellPainting事件。如果(e.ColumnIndex = 0),则表示使用索引为0的列的自定义绘制逻辑。 - Reza Aghaei
很不幸,CheckBoxRenderer 不支持使用与 Windows 主题不同的颜色。 - Reza Aghaei
您可以使用红色填充复选框的矩形。 - Reza Aghaei
先生,请最后帮我做一下这个 :( - user6825121
显示剩余15条评论

0

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

这是我建议您应该做的:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接