ComboBoxes & DataTables
-
Ok i have a DataTable that has 2 columns: ID & Name I have populated a combo box from that data table like so ComboBox1.DataSource = XmlIn.Tables["Names"]; ComboBox1.DisplayMember = "Name"; Now when a user selects a name from the combobox and then clicks a button i need away to return the coresponding ID to that name. I have thought of using a DataView to scan for the name and return the relevent number but was sure there was a tidier way to return the ID value. Can any one help? Thank you.
-
Ok i have a DataTable that has 2 columns: ID & Name I have populated a combo box from that data table like so ComboBox1.DataSource = XmlIn.Tables["Names"]; ComboBox1.DisplayMember = "Name"; Now when a user selects a name from the combobox and then clicks a button i need away to return the coresponding ID to that name. I have thought of using a DataView to scan for the name and return the relevent number but was sure there was a tidier way to return the ID value. Can any one help? Thank you.
set the ValueMember property of the control: ComboBox1.ValueMember = "ID"; ComboBox1.DisplayMember = "Name"; ComboBox1.DataSource = XmlIn.Tables["Names"]; Then, you can get the selected Value by using the SelectedValue property: ComboBox1.SelectedValue; Jon G www.Gizmocoder.com
-
Ok i have a DataTable that has 2 columns: ID & Name I have populated a combo box from that data table like so ComboBox1.DataSource = XmlIn.Tables["Names"]; ComboBox1.DisplayMember = "Name"; Now when a user selects a name from the combobox and then clicks a button i need away to return the coresponding ID to that name. I have thought of using a DataView to scan for the name and return the relevent number but was sure there was a tidier way to return the ID value. Can any one help? Thank you.
just add another line to you code after setting the displaymember - combobox1.ValueMember = XmlIn.Tables["ID"]; then to get the id you could do something like - combobox1.SelectedValue.toString() gavirj