从SELECT Query显示Datagridview复选框状态

时间:2015-08-27 13:16:11

标签: sql vb.net winforms datagridview

您好我想将SELECT查询的结果显示到Datagridview Checkbox Column。我想要的是在从datareader获得结果后看到相应的单元格有/无检查。

这是我的代码:

 Dim selectQ2 = "SELECT Advance FROM EmpTripTBL WHERE [EmpID] ='" & Form4.empidtxt.Text & "'"
    Dim dr As OleDbDataReader
    Dim cmd As New OleDbCommand(selectQ2, con)
    dr = cmd.ExecuteReader()
    dr.Read()
    If dr.HasRows Then
        Dim advstr As String = dr.Item("Advance")

        //  To add Datagridview Checkbox column
        Dim addcolumn As New DataGridViewCheckBoxColumn
        With addcolumn
            .HeaderText = "Advance"
            .Name = "Advance"
            .Width = 50
        End With
        DataGridView1.Columns.Insert(0, addcolumn)

        //To check column from database if it's Yes or No
        //Yes = Check
        //No = Uncheck

        If advstr = "Yes" Then
           //Probably the wrong part.

            For x As Integer = 0 To DataGridView1.Rows.Count - 2
                DataGridView1.Rows(x).Cells(0).Value = True
            Next
        End If
    End If

这是我的数据库表:

|-- CheckboxColumn --|-- COL 2 --|
     Yes                text1
     No                 text2
     Yes                text3

我从Datagridview得到的结果:

Datagridview

1 个答案:

答案 0 :(得分:0)

如果我理解你,那么访问数据库布尔字段的值为true或false ......

If advstr = TRUE Then
       //Probably the wrong part.

        For x As Integer = 0 To DataGridView1.Rows.Count - 2
            DataGridView1.Rows(x).Cells(0).Value = True
        Next
    End If

If advstr = FALSE Then
       //Probably the wrong part.

        For x As Integer = 0 To DataGridView1.Rows.Count - 2
            DataGridView1.Rows(x).Cells(0).Value = FALSE
        Next
    End If