私はvb.netアプリケーションのフォームを持っています。フォームには2つのコンボボックスが含まれています。 combobox5という名前の1つに請求書番号が含まれ、もう1つにcombobox3という名前のパーティコードが含まれています。両方のコンボボックスは、sqldataadapterを使用してpre_loadedされます。コンボボックスの特定の値を選択する方法vb.netでコンボボックスの項目をループすることによって
これで、コンボボックス5のインボイス番号が変更されたときに、コンボボックス3のパーティコードを変更することができます。これをさらに詳しく説明すると、Stockが発行されると、発行されたパーティーを追跡するために、請求コードと一緒にパーティコードが保存されます。在庫が返ってきたら、どの当事者が在庫を返品したかを把握したいと思います。送り状番号が変更されたときにパーティコードが自動的に選択され、その特定の請求書番号に対してデータベースに保存されているものでなければなりません。 ...
私はそうするために次のコードを使用しています:あなたが選択がプログラムで変更されたときのSelectedIndexChangedも解雇されるため、代わりにSelectionChangeCommittedイベントを使用したい
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