Combobox Display/Value

G

Guest

Is it possible to manually provide (display,value) pairs for a combo box??
The only method I'm aware of is bind the "displaymember" and "valuemember"
properties of the combobox to columns in a datatable.

I want to be able to add items to the combobox and be able to give my own
values for the valuemember property...is this possible??

Thanks.
 
S

S.M. Altaf [MVP]

Parveen,

Yes, it is possible.

You start off by creating your own class that contains its own properties, like this:

Public Class Person

Private _strFName As String

Private _strPID As String

Public Property FirstName() As String

Get

Return _strFName

End Get

Set(ByVal Value As String)

_strFName = Value

End Set

End Property

Public Property PersonId() As String

Get

Return _strPID

End Get

Set(ByVal Value As String)

_strPID = Value

End Set

End Property

Public Overrides Function ToString() As String

Return FirstName

End Function

End Class

Next, add to your combobox and set the valuemember and displaymember like so:

Dim PColl(3) As Person

PColl(0) = New Person

PColl(0).FirstName = "Ed"

PColl(0).PersonId = "JFIE663"

PColl(1) = New Person

PColl(1).FirstName = "Rick"

PColl(1).PersonId = "JIUE32"

PColl(2) = New Person

PColl(2).FirstName = "Mike"

PColl(2).PersonId = "KEU98"

PColl(3) = New Person

PColl(3).FirstName = "HomerJSimpson"

PColl(3).PersonId = "ID10T"

For i As Integer = 0 To 3

Me.ComboBox1.Items.Add(PColl(i))

Next

Me.ComboBox1.DisplayMember = "FirstName"

Me.ComboBox1.ValueMember = "PersonId"


HTH
S.M. Altaf [MVP]





--------------------------------------------------------------------------------

All that glitters has a high refractive index.
www.mendhak.com
Is it possible to manually provide (display,value) pairs for a combo box??
The only method I'm aware of is bind the "displaymember" and "valuemember"
properties of the combobox to columns in a datatable.

I want to be able to add items to the combobox and be able to give my own
values for the valuemember property...is this possible??

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top