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? :confused:
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
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. ;)