ComboBox不显示DisplayMember [英] ComboBox not showing DisplayMember
问题描述
我想在组合框中显示问题ID,以便在文本框中重现匹配的问题。然而,不是问题ID出现,我接收这个所有5问题ID:
WCInterface.ucQuestions + QuestionWCInterface.ucQuestions + / p>
我的代码:
Private loaded As Boolean = False
Private Sub ucQuestions_Load(ByVal sender As System.Object,ByVal e As System.EventArgs)句柄MyBase.Load
cmbQuestion.DisplayMember =Question_ID
cmbQuestion.ValueMember =Question_ID
cmbQuestion.DataSource = retrieveQuestions()'当窗体加载
loaded = True
End Sub
Private Sub cmbQuestion_SelectedIndexChanged(ByVal sender As System.Object,ByVal e As System.EventArgs)句柄cmbQuestion.SelectedIndexChanged
如果(loaded)then
cmbQuestion.DisplayMember =Question_ID
cmbQuestion.ValueMember =Question_ID
cmbQuestion.DataSource = Nothing'当表单加载时重置数据源
cmbQuestion.DataSource = retrieveQuestions b End If
End Sub
公共函数retrieveQuestions()作为列表(问题)
Dim typeList作为新列表(问题)
Dim Str as String =SELECT Question_ID,Question_Text FROM Question
Try
使用conn作为新的SqlClient.SqlConnection(DBConnection)
conn.Open()
使用cmdQuery As New SqlClient。使用drResult As SqlClient.SqlDataReader = cmdQuery.ExecuteReader()
虽然drResult.Read
typeList.Add(新问题(drResult(Question_ID),drResult Question_Text)))
End While
结束使用'自动关闭连接
结束使用
结束使用
Catch ex As Exception
MsgBox(问题列表例外:& ex.Message& vbNewLine& Str)
结束尝试
返回typeList
结束函数
对于我如何显示问题ID,谢谢任何建议。
p>您没有发布您的Question类,但DisplayMember和ValueMember字段不匹配Question类中的属性字段:
它应该看起来像这样:
公共类问题
Property QuestionID As Integer
Property QuestionText As String
Public Sub New(q_ID As Integer,q_Text As String)
QuestionID = q_ID
QuestionText = q_Text
End Sub
结束类
然后,您的数据源属性将如下所示:
cmbQuestion.DisplayMember =QuestionID
cmbQuestion.ValueMember =QuestionID
cmbQuestion.DataSource = retrieveQuestions()
I'm trying to display the Question ID's in the combo box, in order to reproduce the matching question in a text box. However rather than the Question ID's appearing, I am receiving this for all 5 question ID's:
WCInterface.ucQuestions+QuestionWCInterface.ucQuestions+Question
My code:
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
I'd appreciate any suggestions as to how I display the Question ID's, thankyou
解决方案You didn't post your Question class, but the DisplayMember and ValueMember fields aren't matching the property fields in the Question class:
It should look something like this:
Public Class Question Property QuestionID As Integer Property QuestionText As String Public Sub New(q_ID As Integer, q_Text As String) QuestionID = q_ID QuestionText = q_Text End Sub End Class
Then your data source properties would look like this:
cmbQuestion.DisplayMember = "QuestionID" cmbQuestion.ValueMember = "QuestionID" cmbQuestion.DataSource = retrieveQuestions()
这篇关于ComboBox不显示DisplayMember的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!