简体   繁体   English

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

[英]How to replace textbox column to checkbox column in datagridview with mysql query

I have this problem where I want to convert textbox column to checkbox in vb.net. 我在将文本框列转换为vb.net中的复选框时遇到了这个问题。 The data is from mysql and I want one column to be displayed as checkbox column. 数据来自mysql,我希望将一列显示为复选框列。

Here is my code so far: 到目前为止,这是我的代码:

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

This is the image: 这是图像:

结果

As you can see Official? 如您所见, Official? column is not checkbox. 列不是复选框。

Now, I want Official? 现在,我要Official? to be checkbox column. 成为复选框列。 The value in Official? Official?的价值Official? column is only 1 and 0. When the value is 1 checkbox should be check otherwise uncheck. 列仅是1和0。当值为1时,应选中复选框,否则取消选中。 How to achieve this? 如何实现呢?

You can add the columns to the DataTable manually specifying the Boolean datatype for the OFFICIAL? 您可以将列手动添加到DataTable中,从而为OFFICIAL指定布尔数据类型? column. 柱。 When the DataTable is bound to the grid it will display check boxes. 当DataTable绑定到网格时,它将显示复选框。

You can pass your connection string directly to the constructor of the connection and pass the command text and connection directly to the constructor of the command. 您可以将连接字符串直接传递给连接的构造函数,并将命令文本和连接直接传递给命令的构造函数。 The Using...End Using blocks ensure that the database objects are closed and disposed even if there is an error. Using ... End Using块可确保即使有错误,也可以关闭和处置数据库对象。

If you need a DataAdapter later to Update the database you can create one then. 如果以后需要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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM