Results 1 to 7 of 7

Thread: combobox

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2002
    Location
    London
    Posts
    678

    combobox

    hi,
    I would like to populate a comboBox with three text values and assign an ID to each one so that when one of them is selected, then an ID for that text value is used later on
    ie.
    combobox1.items.add("value1")
    combobox1.items.add("value2")
    combobox1.items.add("value3")

    tried
    accountTypeComboBox.Items.Add (new ListItem("Item 1", "0"))
    accountTypeComboBox.Items.Add (new ListItem("Item 2", "1"))

    but gives an error on ListItem

    Thanks
    Last edited by fmardani; Oct 10th, 2005 at 06:54 AM.

  2. #2
    Frenzied Member Phill64's Avatar
    Join Date
    Jul 2005
    Location
    Queensland, Australia
    Posts
    1,201

    Re: combobox

    use the index value instead of the text property.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2002
    Location
    London
    Posts
    678

    Re: combobox

    Quote Originally Posted by Phill64
    use the index value instead of the text property.
    But I would like to have the text to be shown and the ID to be retrieved when an item is selected.
    Thanks

  4. #4
    Frenzied Member Phill64's Avatar
    Join Date
    Jul 2005
    Location
    Queensland, Australia
    Posts
    1,201

    Re: combobox

    ComboBox1.SelectedIndex

    this will give you the index (eg: ID) of the selected item in ComboBox1
    it's as simple as that

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2002
    Location
    London
    Posts
    678

    Re: combobox

    Quote Originally Posted by Phill64
    ComboBox1.SelectedIndex

    this will give you the index (eg: ID) of the selected item in ComboBox1
    it's as simple as that
    How do you know which ID is assigned to which text when populating...
    please note the IDs are 1,2,3
    they do not start from 0
    Thanks

  6. #6
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: combobox

    then "id = combobox1.selectedindex + 1"....

  7. #7
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: combobox

    Quote Originally Posted by fmardani
    But I would like to have the text to be shown and the ID to be retrieved when an item is selected.
    Thanks
    Create a datatable then bound it to the combobox.
    i.e
    VB Code:
    1. combobox1.datasource=mytable
    2. combobox1.displaymember="name" <---column name of your datatable
    3. combobox1.valuemember="id" <---column name of your datatable
    When you select an item the returning value is the id that correspand to the name.

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