combobox get value member

Hi

I have done soemthing like this

        int cid = 1;
        string cn ="Company Name";
        cboCompanyID.ValueMember = "Key";
        cboCompanyID.DisplayMember = "Value";
        cboCompanyID.Items.Add(new KeyValuePair<int, string>(cid, cn));

to get a key vale pair in my combobox dropdown.  I now need to get the value item out but can not see it in any properties in cboCompanyID.SelectedItem I see the Key/value pairs in something like [1,General Electric] and could parse it out from there but would think there is a  property with the value.
Charles BaldoSoftware DeveloperAsked:
Who is Participating?
 
numberkruncherConnect With a Mentor Commented:
In your source you are not binding a source:

Try adding your key/value pairs to a list first.

            int cid = 1;
            string cn ="Company Name";
            cboCompanyID.ValueMember = "Key";
            cboCompanyID.DisplayMember = "Value";
 
            // Create data source.
            List<KeyValuePair<int, string>> data = new List<KeyValuePair<int,string>>();
            // Add a value.
            data.Add(new KeyValuePair<int, string>(cid, cn));
 
            // Bind to combo box.
            cboCompanyID.DataSource = data;

Open in new window

0
 
numberkruncherCommented:
Should be able to just use something like the following:
cboCompanyID.SelectedValue

Open in new window

0
 
Éric MoreauSenior .Net ConsultantCommented:
you are mixing 2 things!

when you use ValueMember and DisplayMember, it is beacuse you bind your combo to a datasource.

when you use Items.Add, it is because you feed the combo manually and you need to do it in a loop to add each item to the combo.

Which method are you using?
0
 
numberkruncherCommented:
You can then use:
// Get the selected item.
KeyValuePair<int, string> item = (KeyValuePair<int, string>)cboCompanyID.SelectedItem;
 
 
// Get the selected value.
int value = (int)cboCompanyID.SelectedValue;

Open in new window

0
 
Charles BaldoSoftware DeveloperAuthor Commented:
Thanks Again, Appreciate the detailed source code
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.