我试图在组合框中显示问题 ID,以便在文本框中重现匹配的问题。但是,我收到的不是问题 ID,而是所有 5 个问题 ID:
WCInterface.ucQuestions+问题WCInterface.ucQuestions+问题
我的代码:
Private loaded As Boolean = False
Private Sub ucQuestions_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cmbQuestion.DisplayMember = "Question_ID"
cmbQuestion.ValueMember = "Question_ID"
cmbQuestion.DataSource = retrieveQuestions() 'when form loads
loaded = True
End Sub
Private Sub cmbQuestion_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbQuestion.SelectedIndexChanged
If (loaded) Then
cmbQuestion.DisplayMember = "Question_ID"
cmbQuestion.ValueMember = "Question_ID"
cmbQuestion.DataSource = Nothing 'Resets data source
cmbQuestion.DataSource = retrieveQuestions() 'when form loads
End If
End Sub
Public Function retrieveQuestions() As List(Of Question)
Dim typeList As New List(Of Question)
Dim Str As String = "SELECT Question_ID, Question_Text FROM Question"
Try
Using conn As New SqlClient.SqlConnection(DBConnection)
conn.Open()
Using cmdQuery As New SqlClient.SqlCommand(Str, conn)
Using drResult As SqlClient.SqlDataReader = cmdQuery.ExecuteReader()
While drResult.Read
typeList.Add(New Question(drResult("Question_ID"), drResult("Question_Text")))
End While
End Using 'Automatically closes connection
End Using
End Using
Catch ex As Exception
MsgBox("Question List Exception: " & ex.Message & vbNewLine & Str)
End Try
Return typeList
End Function
对于如何显示问题 ID 的任何建议,我将不胜感激,谢谢