在vb.net中绑定2个访问表中的1个文本框

时间:2017-04-23 09:37:16

标签: vb.net ms-access

我尝试从2个表中绑定1个文本框。

enter image description here

我想点击listbox1中的项目,列出listbox1的所有文本框。当我点击listbox2中的项目时,所有文本框都绑定了listbox2。

我的想法是选中每个列表框中的检查项目。但我无法用这种方式实现。所有文本框总是只为列表框1绑定。我不知道是否有这样做。

这是我的代码

Imports System.Data
Imports System.Data.OleDb

Public Class Form1
    Public cnn As New OleDb.OleDbConnection
    Public cmd As New OleDb.OleDbCommand
    Public da As New OleDb.OleDbDataAdapter
    Public ds As New DataSet
    Public ds2 As New DataSet
    Public dt As New DataTable
    Public dt2 As New DataTable
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cnn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\1_Project\Project_of_VB_Net\AccessDatabase\StudentDatabase.accdb"
        cmd.Connection = cnn
        'get data from table 1 and fill to dataset 1
        cnn.Open()
        cmd.CommandText = "SELECT * FROM StudentData"
        da.SelectCommand = cmd
        ds.Clear()
        da.Fill(ds, "StudentData")
        dt = ds.Tables("StudentData")
        'get data from table 2 and fill to dataset 2
        cmd.CommandText = "SELECT * FROM StudentData2"
        da.SelectCommand = cmd
        ds2.Clear()
        da.Fill(ds2, "StudentData2")
        dt2 = ds2.Tables("StudentData2")
        cnn.Close()
        'binding data to listbox1
        lb1.DataSource = dt
        lb1.DisplayMember = "StudentID"
        'binding data to listbox2
        lb2.DataSource = dt2
        lb2.DisplayMember = "StudentID2"

        If lb1.SelectedIndex <> -1 Then 'check item listbox 1 is selected ?
            'binding textbox for listbox 1
            txtID.DataBindings.Add("Text", dt, "ID")
            txtStudentName.DataBindings.Add("Text", dt, "StudentName")
            txtStudentID.DataBindings.Add("Text", dt, "StudentID")
            txtStudentClass.DataBindings.Add("Text", dt, "StudentClass")
        ElseIf lb2.SelectedIndex <> -1 Then 'check item listbox 1 is selected ?
            'binding textbox for listbox 2
            txtID.DataBindings.Add("Text", dt2, "ID2")
            txtStudentName.DataBindings.Add("Text", dt2, "StudentName2")
            txtStudentID.DataBindings.Add("Text", dt2, "StudentID2")
            txtStudentClass.DataBindings.Add("Text", dt2, "StudentClass2")
        End If
    End Sub
End Class 

解决:

谢谢jmcilhinney,在你的帮助下,我解决了我的问题。你是完全正确的,当用户点击每个列表框上的项目时,我必须使用SelectedIndexChanged事件来检查和绑定数据。

这是我的完整代码

Imports System.Data
Imports System.Data.OleDb

Public Class Form1
    Public cnn As New OleDb.OleDbConnection
    Public cmd As New OleDb.OleDbCommand
    Public da As New OleDb.OleDbDataAdapter
    Public ds As New DataSet
    Public ds2 As New DataSet
    Public dt As New DataTable
    Public dt2 As New DataTable
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cnn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\1_Project\Project_of_VB_Net\AccessDatabase\StudentDatabase.accdb"
        cmd.Connection = cnn
        'get data from table 1 and fill dataset 2
        cnn.Open()
        cmd.CommandText = "SELECT * FROM StudentData"
        da.SelectCommand = cmd
        ds.Clear()
        da.Fill(ds, "StudentData")
        dt = ds.Tables("StudentData")
        'get data from table 2 and fill dataset 2
        cmd.CommandText = "SELECT * FROM StudentData2"
        da.SelectCommand = cmd
        ds2.Clear()
        da.Fill(ds2, "StudentData2")
        dt2 = ds2.Tables("StudentData2")
        cnn.Close()
        'binding data to listbox1
        lb1.DataSource = dt
        lb1.DisplayMember = "StudentID"
        'binding data to listbox2
        lb2.DataSource = dt2
        lb2.DisplayMember = "StudentID2"
    End Sub

    Private Sub lb1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lb1.SelectedIndexChanged
        txtID.DataBindings.Clear()
        txtStudentName.DataBindings.Clear()
        txtStudentID.DataBindings.Clear()
        txtStudentClass.DataBindings.Clear()

        txtID.DataBindings.Add("Text", dt, "ID")
        txtStudentName.DataBindings.Add("Text", dt, "StudentName")
        txtStudentID.DataBindings.Add("Text", dt, "StudentID")
        txtStudentClass.DataBindings.Add("Text", dt, "StudentClass")
    End Sub

    Private Sub lb2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lb2.SelectedIndexChanged
        txtID.DataBindings.Clear()
        txtStudentName.DataBindings.Clear()
        txtStudentID.DataBindings.Clear()
        txtStudentClass.DataBindings.Clear()

        txtID.DataBindings.Add("Text", dt2, "ID2")
        txtStudentName.DataBindings.Add("Text", dt2, "StudentName2")
        txtStudentID.DataBindings.Add("Text", dt2, "StudentID2")
        txtStudentClass.DataBindings.Add("Text", dt2, "StudentClass2")
    End Sub

End Class

0 个答案:

没有答案