Search code examples
vb.netcomboboxvisual-studio-2012selectedvalue

Set combobox's value member programmatically


I've been looking around for this answer. Checked here: How to set a combobox value but I'm not sure if it applies to me (could be wrong, please correct me if I am). I'm using VB.Net, VS2012 and I need to programmatically set the value member of a combobox that is databound.

My code now is as follows (this is from within a loop assigning a bunch of controls values):

    cboCountry.SelectedValue = row.Item("CCCOUNTRY").ToString

This does not assign any selected value. I have also tried:

    cboCountry.SelectedItem = cboCountry.FindString(row.Item("CCCOUNTRY").ToString)

But this does not work either. For this instance:

  1. I have one combobox
  2. It has two values databound in it's valuemember properties, "US", and "CA"
  3. The row item that I'm assigning it is one of those values.

Again, all I need to do is set the selectedvalue programmatically. Any help greatly appreciated!


Solution

  • You're close on your second try -- replace SelectedItem with SelectedIndex:

    cboCountry.SelectedIndex = cboCountry.FindString(row.Item("CCCOUNTRY").ToString)