gpt4 book ai didi

.net - Datagridview 中的方形填充复选框

转载 作者:行者123 更新时间:2023-12-02 03:11:55 26 4
gpt4 key购买 nike

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

表名:ri_closure enter image description here

如上图所示,除了 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

现在是这个样子

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

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

enter image description here

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

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

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

enter image description here

future 帮助的 TYSM

最佳答案

您可以通过处理 CellPainting 来自定义 DataGridViewCheckBox 列的绘制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

这将是结果:

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

关于.net - Datagridview 中的方形填充复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39545483/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com