?
Solved

combobox get value member

Posted on 2009-04-19
5
Medium Priority
?
959 Views
Last Modified: 2013-12-17
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.
0
Comment
Question by:Charles Baldo
  • 3
5 Comments
 
LVL 13

Expert Comment

by:numberkruncher
ID: 24179088
Should be able to just use something like the following:
cboCompanyID.SelectedValue

Open in new window

0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 24179112
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
 
LVL 13

Accepted Solution

by:
numberkruncher earned 2000 total points
ID: 24179115
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
 
LVL 13

Expert Comment

by:numberkruncher
ID: 24179119
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
 

Author Closing Comment

by:Charles Baldo
ID: 31571968
Thanks Again, Appreciate the detailed source code
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month14 days, 6 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question