Datagridview CheckboxColumn着色或(X)未选中时标记

时间:2016-09-13 02:16:46

标签: vb.net datagridview

早上好

我在DataGridView中有一个列生成复选框,相对于我而言,我的表中还有很多列称为2016,2017 and so on,所有这些列都是TinyInt < / p>

现在这里是两个图像:

Table

Datagridview

现在这是我的问题,而不是取消选中如何在DataGridView列中对其进行着色或X标记?

这是填充DataGridView

的代码
Dim sql1 As MySqlCommand = New MySqlCommand("Select * from period_closure", con1)
Dim ds1 As DataSet = New DataSet
Dim adapter1 As MySqlDataAdapter = New MySqlDataAdapter
con1.Open()
adapter1.SelectCommand = sql1
adapter1.Fill(ds1, "MyTable")
DataGridView1.DataSource = ds1.Tables(0)
con1.Close()
ds1.Tables(0).Columns(2).DataType = GetType(Boolean)
Me.DataGridView1.Columns(0).Frozen = True
Dim i As Integer
For i = 0 To DataGridView1.Columns.Count - 1
    DataGridView1.Columns.Item(i).SortMode = DataGridViewColumnSortMode.Programmatic
Next
DataGridView1.Columns("PeriodID").Visible = False
DataGridView1.Columns(0).DefaultCellStyle.BackColor = Color.LightBlue

1 个答案:

答案 0 :(得分:0)

首先下载&#39; X&#39;谷歌中的图像,或创建自己的图像。

然后尝试在您的代码中添加它。

Private Sub CheckBox1_Paint(sender As Object, e As PaintEventArgs) Handles CheckBox1.Paint
 If CheckBox1.Checked = False Then
        Try
            Dim newImage As Image = Image.FromFile("E:\Projects\Library\BER-X.jpg")
            Dim x As Single = 0
            Dim y As Single = 0
            Dim width As Single = CheckBox1.Width 'You can also try this CheckBox1.Width - 3
            Dim height As Single = CheckBox1.Height 'You can also try this CheckBox1.Height - 3
            e.Graphics.DrawImage(newImage, x, y, width, height)
        Catch ex As Exception
        End Try
    End If
End Sub

复选框中的X标记位置不准确,但您可以自行修复。尝试并探索它以适应您的系统。 :)

希望这会对你有所帮助。