3

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

表名:ri_closure 在此处输入图像描述

正如您在图像上方的所有列中看到的那样,除了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

现在这就是它的样子

在此处输入图像描述

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

  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 为未来提供帮助

4

2 回答 2

1

您可以通过DataGridViewCheckBox处理CellPainting. DataGridView然后您可以使用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

这将是结果:

在此处输入图像描述

要在调用后以红色绘制未选中(实际上是混合状态),请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

在此处输入图像描述

于 2016-09-17T10:55:12.323 回答
0

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

这是我建议你应该做的,

  1. 决定要显示的图像而不是复选框(例如绿色勾号)
  2. DataGridView'VirtualMode属性设置为true
  3. DataGridView在将与您的DataTable所有月份列的迭代绑定后,将其设置DataProperptyName为空字符串(这对于触发事件很重要)
  4. DataGridView为的事件创建一个事件处理程序CellValueNeeded,在此事件中,您将在事件参数中接收列索引和行索引。使用它来查找背后的实际值,然后返回绿色刻度图像(使用e.Value)或根据值返回空白图像。
于 2016-09-17T10:45:01.947 回答