Populating a Combobox (with values)

Hi all,

I'm pretty new to programming with vb.net. I'm stuck on a problem:
I need to populate a combobox with items and a value for each item.

for example if it were for countries:
Item - Value
Canada - U
France - E
Egypt - A
and such...

I was able to do such things with ASP, but with .net it doesn't seem the same.
I tried: Combobox1.Items.Add("Canada", "U"), but it didn't work.

Any help would be appreciated.

Thanks...



Answer this question

Populating a Combobox (with values)

  • Craig Lichtenstein - MSFT

    Thanks alot for the reply (and great advice)...
    But for a simple guy like me who's new to the whole .net thing, I wouldn't how to do any of what you've mentioned (I am pretty new to this whole concept of classes, and OOP so I really don't know how I would go around most of what you've said.)

    I have come this far:
    Public Sub FillMenu()
    Dim conn = DBConn() 'a function the opens a connection to my database
    Dim sqlstr As String = "select * from Form_Table"
    Dim rs = DBrs(conn, sqlstr) 'a function that runs the query and defines RS
    Do While rs.read
    ComboBox1.Items.Add(rs("form_name"))
    Loop
    conn.close()
    End Sub

    All I need to do is add values along with the items. The above code works as it fills the combobox and I can display the selected items with the following code:
    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    TextBox2.Text = ComboBox1.SelectedItem
    End Sub
    When a user selects an item from the combobox, I'm trying to get the value displayed in another label or a textbox. But since I can't even get the values in the combobox, I'm completely lost... and helpless...


  • MagicCity33

    OK, you didn't mention that this data was coming from a database. That makes it easier. Just use a DataAdapter to fill a DataTable and then assign the DataTable to the DataSource property of the ComboBox. You then assign the name of the column that you want to display to the DisplayMember property and the name of the code column to the ValueMember property. Then when the user selects an item from the ComboBox the code you want can be retrieved from the SelectedValue property. This is not the most efficient way to do this in terms of performance, but the difference will not be noticeable and it is definitely the easiest way.
  • Matthew Devine

    hi,

    i thought as well to use hashtable but it didn't work and give me exception when i tried the next code

    Dim hshTbl As New Hashtable

    hshTbl.Add("A", "Egypt First")

    hshTbl.Add("u", "canada")

    hshTbl.Add("E", "france")

    Me.ComboBox1.DataSource = hshTbl

    'Me.ComboBox1.ValueMember = hshTbl.Keys 'or"key" as aspx

    'Me.ComboBox1.DisplayMember = hshTbl.Values ' or"value" as aspx

    but this code worked for me


    Public Class Form1
    'this form contain just one combobox

    Dim table As New DataTable
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    table.Columns.Add(
    "key", GetType(String))
    table.Columns.Add(
    "value", GetType(String))
    addrow(
    "A", "Egypt")
    addrow(
    "U", "Canada")
    addrow(
    "E", "France")
    ComboBox1.DataSource = table
    ComboBox1.DisplayMember =
    "value"

    ComboBox1.ValueMember = "key"

    End Sub

    Private Sub addrow(ByVal key As String, ByVal value As String)
    Dim row As DataRow = table.NewRow
    row(
    "key") = key
    row(
    "value") = value
    table.Rows.Add(row)
    End Sub
    End
    Class


    hope this helps



  • Macca

    you are welcome

  • IJay

    okaban wrote:

    Do While rs.read
    ComboBox1.Items.Add(rs("form_name"))
    Loop
    conn.close()
    End Sub

    when you add item you just add the display member the best thing is to creat a table and to add records to it while the rs read like for example to replace the previous part of code

    addrow("A", "Egypt")

    whiththis part

    While rs.Read()

    addrow(rs("keycolumnName"), rs("valueColumnName"))

    End While



  • Gregfig

    Thank you everyone for your help...
    shakalama - your code worked, and saved me from hours of frustration.

    jmcilhinney - thanks again for the dataadapter advice that also worked.

    I have solved my problem with both solutions. Thanks to everyone who took time to help.

    I have been writing ASP and web code for ages but this .net thing is totally different.


  • SonalMS

    You can either create your own type (class or, more likely, structure) that has a property each for the name and the code. You would then create an instance of your type for each item and put them all in an array. You would then assign the array to the DataSource property, the name of the description property to the DisplayMember property and the name of the code property to the ValueMember property. When the user selects a name you can then get the associated code from the SelectedValue property. An alternative would be to use a Hashtable keyed on the names where the values are the codes. You'd then use the selected name in the ComboBox to index the Hashtable to get the corresponding code.
  • Populating a Combobox (with values)