none
想请指点一些代码 RRS feed

  • 问题

  • 我尝试篇写了,当user click comName 时可以在comID呈现当下comName的ID 是什么。但是,我时而拿到对的comName ID时而拿到错的 comName ID想请帮忙看看及指点指点。以下是我的代码。 

             

      Private Sub COMBID()

            Dim constr As String = ("Provider=Microsoft.Jet.Oledb.4.0; Data Source=" + strFullDBPath + ";Jet OLEDB:Database Password=224466;")
            Using con As OleDbConnection = New OleDbConnection(constr)
                Using sda As OleDbDataAdapter = New OleDbDataAdapter("SELECT ID, Name FROM tblEmployeeData", con)
                    Dim dt As DataTable = New DataTable()
                    sda.Fill(dt)

                    comID.DataSource = dt
                    ComboBox1.DataSource = dt
                    ComboBox1.DisplayMember = "Name"
                    ComboBox1.ValueMember = "ID"
                    comID.ValueMember = "ID"

                End Using
            End Using
        End Sub

      

     Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
            If comName.ValueMember <> String.Empty And ComboBox2.SelectedItem IsNot Nothing Then
                ShowValue(ComboBox2.SelectedItem.ToString, Convert.ToInt32(comName.SelectedValue))

            End If
        End Sub

        Private Sub ShowValue(ByVal Name As String, ByVal Id As Integer)
            Dim sql1 As String = "Select " & Name & " from tblEmployeeData where ID=" & Id
            Using con As New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0; Data Source=" + strFullDBPath + ";Jet OLEDB:Database Password=224466;")
                con.Open()
                Using cmd As New OleDbCommand(sql1, con)
                    txtBalance.Text = CStr(cmd.ExecuteScalar)
                    'debug'
                    Using sda As OleDbDataAdapter = New OleDbDataAdapter("SELECT ID, Name FROM tblEmployeeData", con)
                        Dim dt As DataTable = New DataTable()
                        sda.Fill(dt)
                        comID.DataSource = dt
                        comID.DisplayMember = "ID"
                        comID.ValueMember = "Name"
                    End Using
                    'debug'
                End Using
            End Using
        End Sub

        Private Sub Checking_Detail()
            Dim dt As DataTable
            Dim sql As String = "select * from tblEmployeeData"
            dt = loaddate(sql)
            comName.DisplayMember = "Name"
            comName.ValueMember = "ID"
            comName.DataSource = dt
            For Each col As DataColumn In dt.Columns
                ComboBox2.Items.Add(col.ColumnName)
                '’ comID.Items.Add(col.ColumnName)
            Next
        End Sub


    2021年2月1日 9:13

答案

  • Hi,

    经过我的测试,你需要添加一个 BindingSource 并设置其 DataSource 为 dt,然后将 comID 以及 comName 都绑定到该 BindingSource 上。

    改写你的 Checking_Detail方法:

        Dim binding1 As BindingSource = New BindingSource
        Private Sub Checking_Detail()
            Dim dt As DataTable
            Dim sql As String = "select * from tblEmployeeData"
            dt = loaddate(sql)
    
            binding1.DataSource = dt
    
            comName.DataSource = binding1
            comName.DisplayMember = "Name"
            comName.ValueMember = "ID"
    
            comID.DataSource = binding1
            comID.DisplayMember = "ID"
            comID.ValueMember = "Name"
    
            For Each col As DataColumn In dt.Columns
                ComboBox2.Items.Add(col.ColumnName)
            Next
        End Sub
        Private Sub ShowValue(ByVal Name As String, ByVal Id As Integer)
            Dim sql1 As String = "Select " & Name & " from tblEmployeeData where ID=" & Id
            Using con As New OleDbConnection("...")
                con.Open()
                Using cmd As New OleDbCommand(sql1, con)
                    txtBalance.Text = CStr(cmd.ExecuteScalar)
                End Using
            End Using
        End Sub

    希望对你有帮助。

    Best Regards,

    Xingyu Zhao


    Visual Basic and CLR forum will be migrating to a new home on Microsoft Q&A! (VB.NET and CLR) We invite you to post new questions in the new home on Microsoft Q&A ! For more information, please refer to the sticky post(VB.NET and CLR).


    2021年2月2日 5:49
    版主

全部回复

  • Hi christing,

    我有个问题想和你确认一下。

    在Combobox2选择的值 或者 comName选择的值变化以后,是否 comID也要及时同步到对应的值呢?

    >> 我时而拿到对的comName 的ID时而拿到错的

    是否第一次是正确的,然后在 Combobox2选择其他值后,comID没有相应的改变.

    期待你的更新。

    Best Regards,

    Xingyu Zhao


    Visual Basic and CLR forum will be migrating to a new home on Microsoft Q&A! (VB.NET and CLR) We invite you to post new questions in the new home on Microsoft Q&A ! For more information, please refer to the sticky post(VB.NET and CLR).

    2021年2月2日 2:25
    版主
  • @xing yu zhao 感谢你的回复

    在Combobox2选择的值 或者 comName选择的值变化以后,是否 comID也要及时同步到对应的值呢?

    >>在comName选择的值变化以后,comID也要同步对应。

    是否第一次是正确的,然后在 Combobox2选择其他值后,comID没有相应的改变.

    是的

    在此感谢你的回复

    2021年2月2日 3:46
  • Hi,

    经过我的测试,你需要添加一个 BindingSource 并设置其 DataSource 为 dt,然后将 comID 以及 comName 都绑定到该 BindingSource 上。

    改写你的 Checking_Detail方法:

        Dim binding1 As BindingSource = New BindingSource
        Private Sub Checking_Detail()
            Dim dt As DataTable
            Dim sql As String = "select * from tblEmployeeData"
            dt = loaddate(sql)
    
            binding1.DataSource = dt
    
            comName.DataSource = binding1
            comName.DisplayMember = "Name"
            comName.ValueMember = "ID"
    
            comID.DataSource = binding1
            comID.DisplayMember = "ID"
            comID.ValueMember = "Name"
    
            For Each col As DataColumn In dt.Columns
                ComboBox2.Items.Add(col.ColumnName)
            Next
        End Sub
        Private Sub ShowValue(ByVal Name As String, ByVal Id As Integer)
            Dim sql1 As String = "Select " & Name & " from tblEmployeeData where ID=" & Id
            Using con As New OleDbConnection("...")
                con.Open()
                Using cmd As New OleDbCommand(sql1, con)
                    txtBalance.Text = CStr(cmd.ExecuteScalar)
                End Using
            End Using
        End Sub

    希望对你有帮助。

    Best Regards,

    Xingyu Zhao


    Visual Basic and CLR forum will be migrating to a new home on Microsoft Q&A! (VB.NET and CLR) We invite you to post new questions in the new home on Microsoft Q&A ! For more information, please refer to the sticky post(VB.NET and CLR).


    2021年2月2日 5:49
    版主