如何在Visual Basic 2010中将每个项目的数据值添加到组合框?
像html下拉框一样.
或者无论如何要为每个项目添加值?
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,那么你的组合框必须是数据绑定的.
原文链接:https://www.f2er.com/vb/255584.html要设置组合框:
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