Binding the selected value from a combobox to a member of a class.

Posted by CM on Stack Overflow See other posts from Stack Overflow or by CM
Published on 2010-01-18T21:39:16Z Indexed on 2010/04/22 12:03 UTC
Read the original article Hit count: 183

Filed under:
|
|

I have a combobox that is bound to a an instance of a class. I need to get the user's selection ID of the combobox and set a class property equal to it.

For example, here is the class:

public class robot
{
    private string _ID;
    private string _name;
    private string _configFile;
    [XmlElement("hardware")]
    public hardware[] hardware;

    public string ID
    {
        get { return _ID; }
        set { _ID = value; }
    }
    public string name
    {
        get { return _name; }
        set { _name = value; }
    }
    public string configFile
    {
        get { return _configFile; }
        set { _configFile = value; }
    }
}

Now here is the code to bind the combobox to an instance of that class. This display's the name of each robot in the array in the combobox.

    private void SetupDevicesComboBox()
    {
        robot[] robot = CommConfig.robot;
        cmbDevices.DataSource = robot;
        cmbDevices.DisplayMember = "name";
        cmbDevices.ValueMember = "ID";
    }

But now I can't seem to take what the user selects and use it. How do I use the "ID" of what the user select's from the combobox?

Settings.selectedRobotID = cmbDevices.ValueMember;  //This just generates "ID" regardless of what is selected.

I also tried

Settings.selectedRobotID = cmbDevices.SelectedItem.ToString();  //This just generates "CommConfig.robot"

Thanks

© Stack Overflow or respective owner

Related posts about c#

Related posts about data