2017-09-03 53 views
1

我在這裏有一個問題在vb.net我需要獲取組合框值的值到文本框。但它只顯示組合框的最後一個索引。/ADODB vb.net組合框值到文本框工作在最後一個索引

//這裏是我的代碼

Private Sub cboname_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboname.SelectedIndexChanged 
    strSql = "SELECT * FROM tblCashier WHERE ProductCode= '" & cboname.Text & "' " 

    Do Until myRecord.EOF 
     txtname.Text = myRecord.Fields("Product").Value 
     myRecord.MoveNext() 
    Loop 



    Call executeQuery2(strSql) 
    Call getRecord() 
End Sub 

// ADODB CONNECTIONS

Dim strSql As String 
Dim myRecord As New ADODB.Recordset 

// COMBO框中的值

Sub fillcombo() 
    strSql = "SELECT * FROM tblCashier" 

    Do While Not myRecord.EOF 
     cboname.Items.Add(myRecord.Fields("ProductCode").Value) 
     myRecord.MoveNext() 
    Loop 

    cboname.Refresh() 
    Call executeQuery(strSql) 
    Call getRecord() 
End Sub 

//的Form_Load

Private Sub myPOS_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
    Call openConnection() 
    Call getRecord() 
    Call fillcombo() 


End Sub 
+0

你爲什麼首先使用ADODB?你爲什麼不使用ADO.NET?你應該做的是使用數據適配器填充一個DataTable,然後將它綁定到ComboBox和TextBox。隨着用戶選擇項目,您不需要任何代碼來填充「文本框」。 – jmcilhinney

+0

至於具體的問題,考慮到你的'SelectedIndexChanged'事件處理程序中有一個循環,它專門移動到'Recordset'的末尾,爲什麼除了獲取最後一個項目之外,你還期望什麼? – jmcilhinney

回答

0

這樣的事情應該做這樣的事:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
    Using adapter As New OleDbDataAdapter("SQL query here", "connection string here") 
     Dim table As New DataTable 

     adapter.Fill(table) 

     BindingSource1.DataSource = table 

     With ComboBox1 
      .DisplayMember = "ColumnNameToDisplay" 
      .ValueMember = "PrimaryKeyColumnName" 
      .DataSource = BindingSource1 
     End With 

     TextBox1.DataBindings.Add("Text", BindingSource1, "OtherColumnNameToDisplay") 
    End Using 
End Sub 

TextBox將自動作爲ComboBox變化選擇更新。