vb.net – 如何为组合框项添加值

前端之家收集整理的这篇文章主要介绍了vb.net – 如何为组合框项添加值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何在Visual Basic 2010中将每个项目的数据值添加到组合框?

像html下拉框一样.

或者无论如何要为每个项目添加值?

我正在从MySQL数据库添加项目,如下所示:

Command = New MysqLCommand("SELECT * FROM `maillist` WHERE l_id = '" & id & "'",connection)

Command.CommandTimeout = 30
Reader = Command.ExecuteReader()
If Reader.HasRows = True Then
    While Reader.Read()
        ComboBox1.Items.Add(Reader("name"))
    End While
End If

我需要添加Reader(“ID”)作为每个项目的值…

如果你想使用SelectedValue,那么你的组合框必须是数据绑定的.

要设置组合框:

ComboBox1.DataSource = GetMailItems()
ComboBox1.DisplayMember = "Name"
ComboBox1.ValueMember = "ID"

获取数据:

Function GetMailItems() As List(Of MailItem)

    Dim mailItems = New List(Of MailItem)

    Command = New MysqLCommand("SELECT * FROM `maillist` WHERE l_id = '" & id & "'",connection)
    Command.CommandTimeout = 30
    Reader = Command.ExecuteReader()

    If Reader.HasRows = True Then
        While Reader.Read()
            mailItems.Add(New MailItem(Reader("ID"),Reader("name")))
        End While
    End If

    Return mailItems

End Function

Public Class MailItem

    Public Sub New(ByVal id As Integer,ByVal name As String)
        mID = id
        mName = name
    End Sub

    Private mID As Integer
    Public Property ID() As Integer
        Get
            Return mID
        End Get
        Set(ByVal value As Integer)
            mID = value
        End Set
    End Property

    Private mName As String
    Public Property Name() As String
        Get
            Return mName
        End Get
        Set(ByVal value As String)
            mName = value
        End Set
    End Property

End Class
原文链接:https://www.f2er.com/vb/255584.html

猜你在找的VB相关文章