如何在vb.net中的数据库中设置datagridview复选框值

时间:2018-02-09 10:58:32

标签: mysql vb.net datagridview

我想从数据库设置datagridview复选框值,但我不知道如何。

MySql查询:

SELECT students.Name, students.Class, students.Feedback, IF(students.Feedback = 0, 1, 0) 
NotFeedBack FROM students;

结果是:

====================================================
|Name          |Class        |Feedback |NotFeedBack|
====================================================
|Sandra        |1A           |1        |0          |
|Layla         |2B           |1        |0          |
|Nagisa        |3E           |0        |1          |
|Akabane       |3E           |1        |0          |
====================================================

在vb.net我是datagridview1,这里我从数据库声明并设置值:

Sub show_header()
    datagridview1.Columns.Add("Name", "Name")
    datagridview1.Columns.Add("Class", "Class")
    Dim chk As New DataGridViewCheckBoxColumn()
    datagridview1.Columns.Add(chk)
    chk.HeaderText = "Feedback"
    chk.Name = "Feedback"
    Dim ch As New DataGridViewCheckBoxColumn()
    datagridview1.Columns.Add(ch)
    ch.HeaderText = "NotFeedBack"
    ch.Name = "ch"
End Sub

Sub LoadData()
    dgvOTSum.Rows.Clear()
    dgvOTSum.Refresh()
    Dim tblot As New tblot
    Dim DtTable As New DataTable
    DtTable = tblot.DetailOT
    With DtTable
        For i = 0 To DtTable.Rows.Count - 1
            dgvOTSum.Rows.Add(.Rows(i).Item(0).ToString(), .Rows(i).Item(1).ToString(), _
                             .Rows(i).Item(2).ToString(), .Rows(i).Item(3).ToString())
        Next
    End With
End Sub

我想要这样的结果:

enter image description here

2 个答案:

答案 0 :(得分:0)

首先你应该尝试数据绑定。

其次你可以尝试:

ch.TrueValue = 1
ch.FalseValue = 0

答案 1 :(得分:0)

#Region "My declare variable (Class Feedback)"
Private _Name As String
Private _Class As String
Private _Feedback As Boolean
#End Region

#Region "Public Property"
Public Property Name As String
Get
    Return _Name
End Get
Set (value As String)
    _Name = value
End Set
End Property

Public Property Class As String
Get
    Return _Class
End Get
Set (value As String)
    _Class = value
End Set
End Property

Public Property Feedback As Boolean
Get
    Return _Feedback
End Get
Set (value As Boolean)
    _Feedback = value
End Set
End Property
#End Region

功能

Public Function DetailOT() As DataTable
    Dim sql = "SELECT students.Name, students.Class, students.Feedback, IF(students.Feedback = 0, 1, 0)
NotFeedBack FROM students;"
Try
    dtTable = Controldata.SelectQuery(sql)
Catch ex As Exception
    MsgBox(ex.ToString)
End Try
Return dtTable

结束功能

Form1中
Sub show_header()
datagridview1.Columns.Add("Name", "Name")
datagridview1.Columns.Add("Class", "Class")
Dim chk As New DataGridViewCheckBoxColumn()
datagridview1.Columns.Add(chk)
chk.HeaderText = "Feedback"
chk.Name = "Feedback"
Dim ch As New DataGridViewCheckBoxColumn()
datagridview1.Columns.Add(ch)
ch.HeaderText = "NotFeedBack"
ch.Name = "ch"
End Sub

Sub LoadData()
dgvOTSum.Rows.Clear()
dgvOTSum.Refresh()
Dim tblot As New tblot
Dim DtTable As New DataTable
DtTable = tblot.DetailOT
With DtTable
    For i = 0 To DtTable.Rows.Count - 1
        dgvOTSum.Rows.Add(.Rows(i).Item(0).ToString(), .Rows(i).Item(1).ToString(), _
                         .Rows(i).Item(2).ToString(), .Rows(i).Item(3).ToString())
    Next
End With
End Sub


Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    show_header()
    LoadData()
End Sub