|
-
Dec 23rd, 2004, 11:18 AM
#1
Thread Starter
Frenzied Member
ComboBox: DataValueField with an Array
We all know you can populate a ComboBox with an array like so:
VB Code:
Dim arColours As New ArrayList(3)
With arColours
.Add("Black Magic Pearl")
.Add("Imola Yellow")
.Add("Jazz Blue")
End With
cboColour.DataSource = arColours
cboColour.DataBind()
But what if you want the ComboBox to have both the DataValueField and DataTextField populated? The above method only populates the DataTextField.
I only have 3 values that populate the ComboBox, and they are static, so it makes no sense to put them in a table and retrieve them into the ComboBox using a DataSet (the normal way to populate both fields).
Can it be done? Or am i outta luck?
~Peter

-
Dec 23rd, 2004, 11:24 AM
#2
Addicted Member
Re: ComboBox: DataValueField with an Array
It sure can:
VB Code:
Dim arColoursText As New ArrayList(3)
Dim arColoursValue As New ArrayList(3)
Dim liAdd As ListItem
Dim i As Int16
With arColoursText
.Add("Black Magic Pearl")
.Add("Imola Yellow")
.Add("Jazz Blue")
End With
With arColoursValue
.Add("1")
.Add("2")
.Add("3")
End With
For i = 0 To Ubound(arColoursValue) - 1
liAdd = New ListItem
liAdd.Text = arColoursText(i)
liAdd.Value = arColoursValue(i)
DropDownList1.Items.Add(liAdd)
Next
-
Dec 23rd, 2004, 11:46 AM
#3
Thread Starter
Frenzied Member
Re: ComboBox: DataValueField with an Array
I was thinking the answer involved a multi-dimensional array, but that works too.
I actually just streamlined the code into 8 lines, and it works great. Thanks.
~Peter

Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|