gpt4 book ai didi

mysql - 如何使用mysql查询将datagridview中的文本框列替换为复选框列

转载 作者:行者123 更新时间:2023-11-29 15:44:11 25 4
gpt4 key购买 nike

我有这个问题,我想将 vb.net 中的文本框列转换为复选框。数据来自mysql,我希望将一列显示为复选框列。

这是迄今为止我的代码:

Private Sub data_load()

Dim mycommand As New MySqlCommand
Dim myadapter As New MySqlDataAdapter
Dim mydata As New DataTable

conn = New MySqlConnection
conn.ConnectionString = connstr

conn.Open()

mycommand.Connection = conn
mycommand.CommandText = "SELECT locator_id, name AS 'NAME', date AS 'DATE', destination AS 'DESTINATION', time_leave AS 'TIME LEAVE', time_return AS 'TIME RETURN', isOfficial AS 'OFFICIAL?' FROM locator_table ORDER BY locator_id DESC"

myadapter.SelectCommand = mycommand
myadapter.Fill(mydata)

DataGridView1.DataSource = mydata
DataGridView1.Refresh()
End Sub

这是图片:

result

如您所见,Official? 列不是复选框。

现在,我希望 Official? 成为复选框列。 Official? 列中的值只有 1 和 0。当值为 1 时,应选中复选框,否则取消选中。如何实现这一目标?

最佳答案

您可以手动将列添加到数据表中,并为 OFFICIAL 指定 bool 数据类型吗?柱子。当 DataTable 绑定(bind)到网格时,它将显示复选框。

您可以将连接字符串直接传递给连接的构造函数,并将命令文本和连接直接传递给命令的构造函数。即使出现错误,Using...End using block 也可确保关闭并处置数据库对象。

如果您稍后需要 DataAdapter 来更新数据库,您可以创建一个。

Private Sub data_load()
Dim mydata As New DataTable
mydata.Columns.Add("locator_id", GetType(Integer))
mydata.Columns.Add("NAME", GetType(String))
mydata.Columns.Add("DATE", GetType(Date))
mydata.Columns.Add("DESTINATION", GetType(String))
mydata.Columns.Add("TIME LEAVE", GetType(String))
mydata.Columns.Add("TIME RETURN", GetType(String))
mydata.Columns.Add("OFFICIAL?", GetType(Boolean))
Using conn As New MySqlConnection(constr)
Using mycommand As New MySqlCommand("SELECT locator_id, name AS 'NAME', date AS 'DATE', destination AS 'DESTINATION', time_leave AS 'TIME LEAVE', time_return AS 'TIME RETURN', isOfficial AS 'OFFICIAL?' FROM locator_table ORDER BY locator_id DESC", conn)
conn.Open()
mydata.Load(mycommand.ExecuteReader)
End Using
End Using
DataGridView1.DataSource = mydata
End Sub

关于mysql - 如何使用mysql查询将datagridview中的文本框列替换为复选框列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57263619/

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