How do I separate Combobox values from display at design time.

Posted on 2007-10-08
Last Modified: 2008-01-09
Using c# 2.0, vs2005 Is there a  way to separate a Combobox's display value from its data value at design time without using a database?  Or perhaps a way to make it have two columns?  I need to store a key value and display a different value like html select list.  I have a small amount of values (10) that I need to just input at design time.  It needs to be light weight.
Question by:fbk2000
    LVL 11

    Accepted Solution

    use can use hashtable and assign it as the combobox datasource
    LVL 11

    Expert Comment


    Hashtable hLang = new Hashtable();
    hLang.Add("1", "ASP.NET");
    hLang.Add("2", "C#");
    hLang.Add("3", "VBNET");

    cbo1.DataSource = hLang;
    LVL 11

    Expert Comment

    U can also use DataTable
    LVL 11

    Expert Comment

    Here is ur final code:

                DataTable dt = new DataTable("data");

                dt.Rows.Add(new object[] { "key1", "value1" });
                dt.Rows.Add(new object[] { "key2", "value2" });

                comboBox1.DataSource = dt;
                comboBox1.DisplayMember = "key";
                comboBox1.ValueMember = "value";

            private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)

    In combobox it displays "key1,key2"

    In the event for value u will get "value1,value2"
    LVL 26

    Expert Comment

    by:Anurag Thakur
    hi, if you want to attach the key/value pairs to the combobox at design time both the approaches suggested by udhayakumard are good. as you also say that the no. of items in the combobox wont be more than 10, i will strongly recommend using the hashtable approach as it will be more efficient than the datatable approach.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Live - One-on-One C# Help from Top Experts

    Solve your toughest problems, fast.
    C# experts are online now and ready to help you.

    Article by: Najam
    Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    761 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

    8 Experts available now in Live!

    Get 1:1 Help Now