下拉列表未显示正确的值

时间:2015-05-06 19:22:41

标签: vb.net winforms

这是我的班级

Public Class TitleV_List
    Public Full_Name As String
    Public ID As Integer
    Sub New(ByVal Full_Name As String, ByVal ID As Integer)
        Me.Full_Name = Full_Name
        Me.ID = ID
    End Sub
End Class

这是我的代码:

Dim TitleVList As New List(Of TitleV_List)


con = New SqlConnection(connectionString)

sql = "Select ID, Full_Name FROM cts_TitleV_Leads WHERE IsActive = 1"

If con.State = ConnectionState.Closed Then
    con.Open()
End If

cmd.CommandType = CommandType.Text
cmd.Connection = con
cmd.CommandText = sql

dr = cmd.ExecuteReader

If dr.HasRows = True Then
    While dr.Read

        TitleVList.Add(New TitleV_List(dr("Full_Name"), dr("ID")))
    End While
End If

If con.State = ConnectionState.Open Then
    con.Close()
    dr.Close()
End If

ddlpropTitleVlist.DataSource = TitleVList

我检查代码并且正确的值会进入下拉列表,但是当我点击下拉箭头时,屏幕上的所有值都是: 'CTSnet.TitleV_List'

项目名称是CTSnet。我做错了什么????

1 个答案:

答案 0 :(得分:2)

你需要做两件事来完成这项工作。首先,为ddlpropTitleVlist设置DisplayMember和ValueMember。设置DataSource后就像下面这样:

ddlpropTitleVlist.DisplayMember = "Full_Name"
ddlpropTitleVlist.ValueMember = "ID"

此外,您需要将这些成员指向TitleV_List类的属性

Public Property Full_Name As String
Public Property ID As Integer

(虽然我们正在讨论这些内容,但我认为你应该从该类名中删除" _List"因为它不是任何类型的列表。也许称之为TitleV_Item然后你的TitleVList会建议一个项目列表,而不是错误地建议一个列表列表。)