0
我的vb.net應用程序中有一個窗體,用於獲取關於返回的庫存的數據。該表單包含兩個組合框。一個名爲combobox5的包含發票號碼,另一個名爲combobox3包含派對代碼。兩個組合框都是使用sqldataadapter預加載的。如何通過循環瀏覽vb.net中的組合框項來選擇組合框上的特定值
現在我想要的是在combobox5中更改發票號碼時更改combobox3中的派對代碼。進一步詳細闡述,當股票發行時,派代碼與發票號碼一起存儲以跟蹤哪一方是股票發行。現在,當股票被退回時,我想跟蹤哪一方已經退回股票,我希望當發票號碼被更改時應該自動選擇派代碼,並且它應該是針對該特定發票號碼存儲在數據庫中的內容。 ...
我用下面的代碼這樣做:
Private Sub ComboBox5_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox5.SelectedIndexChanged
' defines a new connection to the database
Dim con As New SqlConnection("Data Source=TAHA;Initial Catalog=ADT;Integrated Security=True")
con.Open()
If ComboBox5.SelectedIndex = 0 Then
ComboBox3.Enabled = True
If Not ComboBox3.Items.Count = 0 Then
ComboBox3.SelectedIndex = 0
End If
Else
Me.ComboBox3.Enabled = False
Me.ComboBox3.BackColor = Color.White
Me.ComboBox3.ForeColor = Color.Black
Dim invoices As New SqlCommand("select invoice_no, party_code from Outgoing_Invoice group by invoice_no, party_code", con)
Dim reader As SqlDataReader = invoices.ExecuteReader
While reader.Read
Dim cnt, i As Integer
cnt = Me.ComboBox3.Items.Count
If Me.ComboBox5.SelectedItem.ToString.Trim = reader("invoice_no").ToString.Trim Then
If Not cnt = 0 Then
For i = 0 To cnt - 1
If Me.ComboBox3.Items.Item(i).ToString.Trim.Contains(reader("party_code").ToString.Trim) Then 'here i have also used equals instead of contains but that too doesn't work
Me.ComboBox3.SelectedIndex = i
Exit For
End If
Next
End If
End If
End While
reader.Close()
End If
con.Close()
End Sub