combobox get value member

Posted on 2009-04-19
Last Modified: 2013-12-17

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.
Question by:charlesbaldo
    LVL 13

    Expert Comment

    Should be able to just use something like the following:

    Open in new window

    LVL 69

    Expert Comment

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

    Accepted Solution

    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

    LVL 13

    Expert Comment

    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


    Author Closing Comment

    Thanks Again, Appreciate the detailed source code

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now