Assign ID to Combobox

  • 14 years ago

    I am trying to assign a database ID field to possibly the index of the combobox?


    For example:


    I have 2 fields in a dataset


    ds("ID")
    ds("Item")


    The ID i would like to set as the index of the value Item when assigning to a combobox
    Rather than using below:
    cmbName.Items.Add(ds("ID") + " - " +  ("Item"));


    Thanks

  • 14 years ago

    You assign the DataTable to the DataSource property, the name of the column to display to the DisplayMember and the name of the ID column to the ValueMember:

    Code:
    myComboBox.DataSource = myDataSet.Tables("NameOfTable")
    myComboBox.DisplayMember = "NameOfColumnToDisplay"
    myComboBox.ValueMember = "NameOfIDColumn"
    Then, when the user selects an item in the ComboBox, you get the corresponding ID from the SelectedValue property.

  • 14 years ago

    cmbName.DataValueField="ID";

    cmbName.DataTextField = "Item";

     

  • 14 years ago

    bb2004 wrote:

    cmbName.DataValueField="ID";

    cmbName.DataTextField = "Item";

     

    You're confusing the WinForms ComboBox with the WebForms DropDownList.  The ComboBox doesn't have those properties.

  • 14 years ago

    You are correct. For the Combobox I used the below:

     

    this.cmbDelete.ValueMember = (Convert.ToString(dr["ID"]));

    this.cmbDelete.Items.Add(Convert.ToString(dr["GOOD_SAYING"]));

     

    Thanks

     

     

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.” - Donald Knuth