0
我有一個名爲EmployedCombobox1的「下拉」樣式組合框。我試圖讓EmployedComobox1根據寫在另一個表單上的文本框中的文本自動選擇一個值。基於Textbox.Text選擇組合框值
下面的代碼是我EmployedCombobox的填充方式:
Private Sub getinstitutionname(ByVal p_institution1_id As Integer)
If sConnection.State = ConnectionState.Closed Then
sConnection.ConnectionString = Search.sqlConnect
sConnection.Open()
End If
Dim sqlAdapter As New MySqlDataAdapter
Dim sqlCommand As New MySqlCommand
Dim sqlTable As New DataTable
Dim InstitutionName As String
Dim sqlText As String = "select * from institution order by institution_name"
Dim InstitutionID As Integer
With sqlCommand
.CommandText = sqlText
.Connection = sConnection
End With
With sqlAdapter
.SelectCommand = sqlCommand
.Fill(sqlTable)
End With
EmployedComboBox1.Items.Clear()
EmployedComboBox1.SelectedIndex = -1
For i = 0 To sqlTable.Rows.Count - 1
InstitutionName = sqlTable.Rows(i)("institution_name")
InstitutionID = sqlTable.Rows(i)("institution_id")
EmployedComboBox1.Items.Add(InstitutionName)
If p_institution1_id = InstitutionID Then
EmployedComboBox1.SelectedIndex = i
End If
Next
sqlTable.Dispose()
sqlCommand.Dispose()
sqlAdapter.Dispose()
接着,下面的代碼是我試圖自動選擇基於斷值textbox.text:
Dim sqlAdapter1 As New MySqlDataAdapter
Dim sqlCommand1 As New MySqlCommand
Dim sqlTable1 As New DataTable
Dim sqlText1 As String = "select institution_name from institution where institution_name='" & Institution.InstitutionNameTextBox.Text & "'"
If Search.debugging = True Then
MsgBox(sqlText1)
End If
With sqlCommand1
.CommandText = sqlText1
.Connection = sConnection
End With
With sqlAdapter1
.SelectCommand = sqlCommand1
.Fill(sqlTable1)
End With
For i = 0 To sqlTable1.Rows.Count - 1
Me.EmployedComboBox1.SelectedItem = (sqlTable1.Rows(i)("institution_name"))
Next
sqlTable1.Dispose()
sqlCommand1.Dispose()
sqlAdapter1.Dispose()
當我運行第二個代碼,當在文本框中寫入文本時,在組合框中沒有任何選擇。
這條線困擾我。我會首先創建一個字符串,以確保它位於組合項的第一個內: – Kat
執行類似Dim y =(sqlTable1.Rows(i)(「institution_name」))。 – Kat
這將給我一個錯誤,指出「從字符串轉換爲整型無效」 – AznDevil92