Results 1 to 3 of 3

Thread: ComboBox: DataValueField with an Array

  1. #1

    Thread Starter
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Question ComboBox: DataValueField with an Array

    We all know you can populate a ComboBox with an array like so:
    VB Code:
    1. Dim arColours As New ArrayList(3)
    2.         With arColours
    3.             .Add("Black Magic Pearl")
    4.             .Add("Imola Yellow")
    5.             .Add("Jazz Blue")
    6.         End With
    7.         cboColour.DataSource = arColours
    8.         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


  2. #2
    Addicted Member rdove's Avatar
    Join Date
    Dec 2002
    Location
    Indianapolis
    Posts
    251

    Re: ComboBox: DataValueField with an Array

    It sure can:

    VB Code:
    1. Dim arColoursText As New ArrayList(3)
    2. Dim arColoursValue As New ArrayList(3)
    3. Dim liAdd As ListItem
    4. Dim i As Int16
    5.  
    6. With arColoursText
    7.      .Add("Black Magic Pearl")
    8.      .Add("Imola Yellow")
    9.      .Add("Jazz Blue")
    10. End With
    11.  
    12. With arColoursValue
    13.      .Add("1")
    14.      .Add("2")
    15.      .Add("3")
    16. End With
    17.  
    18. For i = 0 To Ubound(arColoursValue) - 1
    19.      liAdd = New ListItem
    20.      liAdd.Text = arColoursText(i)
    21.      liAdd.Value = arColoursValue(i)
    22.      DropDownList1.Items.Add(liAdd)
    23. Next
    ~Ryan





    Have I helped you? Please Rate my posts.

  3. #3

    Thread Starter
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Smile 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
  •  



Click Here to Expand Forum to Full Width