Click here to Skip to main content
13,248,295 members (36,780 online)
Click here to Skip to main content
Add your own
alternative version


28 bookmarked
Posted 15 Jun 2007

How Do I Put Display and Value in Combobox/Listbox Separately?

, 15 Jun 2007
Rate this:
Please Sign up or sign in to vote.
This article demonstrates how to use object as items in combobox and listbox and manipulate them.

Screenshot - ComboBoxDemo1.png

Screenshot - ComboBoxDemo2.png


This article demonstrates two easy ways to use separate display and value for listbox control and combobox control. These controls take type object entities for items. Here it is demonstrated how to manipulate this to get the desired result.


Several times, people come to me saying that they want to display some value in a combobox and on selection, they want to retrieve some other value which is not visible to the user. I found most of the beginners face trouble in this scenario. So I thought why not put this solution on The Code Project so that a beginner user can resolve this common problem.

Basic Idea

When an object is added as an item to these controls, these controls call the ToString() method to get the display. So one can easily manipulate the fact that when one adds object as Item then control will get display text from the Object's ToString() method but item still maintains state of that object. So these items can be used to retrieve the entire object as it is. The second approach is simply binding the DataSource to the control and setting the column names for display and value purposes. The noticeable point is you can have a DataTable with large number of columns out of which one column can be used to display text and one column can be used for value member.

Using the Code

The code is pretty simple and self explanatory. ItemObject is the class which plays the role of giving display text to the control.

public class ItemObject
    private string key;
    private object valueOfKey;

    /// <span class="code-SummaryComment"><summary></span>
    /// Overloaded constructor.
    /// <span class="code-SummaryComment"></summary></span>
    /// <span class="code-SummaryComment"><param name="key">Key of object.</param></span>
    /// <span class="code-SummaryComment"><param name="valueOfKey">Value of object.</param></span>
    public ItemObject(string key, object valueOfKey)
        this.key = key;
        this.valueOfKey = valueOfKey;

    /// <span class="code-SummaryComment"><summary></span>
    /// Default constructor
    /// <span class="code-SummaryComment"></summary></span>
    public ItemObject()
        key = string.Empty;
        valueOfKey = string.Empty;

    ///<span class="code-SummaryComment"><summary></span>
    ///Returns a <span class="code-SummaryComment"><see cref="T:System.String"></see> that represents the current </span>
    ///<span class="code-SummaryComment"><see cref="T:System.Object"></span>
    /// <span class="code-SummaryComment"></see>.</span>
    ///<span class="code-SummaryComment"></summary></span>
    ///<span class="code-SummaryComment"><returns></span>
    ///A <span class="code-SummaryComment"><see cref="T:System.String"></see> that represents the current </span>
    ///<span class="code-SummaryComment"><see cref="T:System.Object"></span>
    /// <span class="code-SummaryComment"></see>.</span>
    ///<span class="code-SummaryComment"></returns></span>
    public override string ToString()
        return key;

    ///<span class="code-SummaryComment"><summary></span>
    ///Serves as a hash function for a particular type. 
    ///<span class="code-SummaryComment"></summary></span>
    ///<span class="code-SummaryComment"><returns></span>
    ///A hash code for the current <span class="code-SummaryComment"><see cref="T:System.Object"></see>.</span>
    ///<span class="code-SummaryComment"></returns></span>
    public override int GetHashCode()
        return ToString().GetHashCode();

    /// <span class="code-SummaryComment"><summary></span>
    /// Gets or sets Key of object.
    /// <span class="code-SummaryComment"></summary></span>
    public string Key
        get { return key; }
        set { key = value; }

    /// <span class="code-SummaryComment"><summary></span>
    /// Gets or sets Value of object.
    /// <span class="code-SummaryComment"></summary></span>
    public object ValueOfKey
        get { return valueOfKey; }
        set { valueOfKey = value; }

Points of Interest

ItemObject class is the container object for the above class. You can design your own container object for your requirement.

ToString() method is the key here so whenever ToString() is called from an application, it will return the Key of the object. This solves the display purpose. So simple!!!

You can use other types of DataSource also with these controls.


  • 16th June, 2007: First release of the code

If you like an article like this which solves a simple development problem, please give feedback.

About Proteans Software Solutions

Proteans is an outsourcing company focusing on software product development and business application development on Microsoft Technology Platform. Proteans partners with Independent Software Vendors (ISVs), System Integrators and IT teams of businesses to develop software products. Our technology focus and significant experience in software product development - designing, building, and releasing world-class, robust and scalable software products help us to reduce time-to-market, cut costs, reduce business risk and improve overall business results for our customers. Proteans expertise's in development using Microsoft .NET technologies.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Manish Ranjan Kumar
Software Developer (Senior)
India India
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Kanasz Robert27-Sep-12 11:53
mvpKanasz Robert27-Sep-12 11:53 
QuestionHow to remove objects once they are in Pin
kboyette16-Aug-07 6:19
memberkboyette16-Aug-07 6:19 
GeneralUseful Arcticle Pin
Nihar_CodePro9-Jul-07 22:52
memberNihar_CodePro9-Jul-07 22:52 
GeneralNice article Pin
gajatko16-Jun-07 5:00
membergajatko16-Jun-07 5:00 
QuestionDoesn't work with other objects? Pin
sharpiesharpie16-Jun-07 1:52
membersharpiesharpie16-Jun-07 1:52 
AnswerRe: Doesn't work with other objects? Pin
Brad Bruce16-Jun-07 3:08
memberBrad Bruce16-Jun-07 3:08 
GeneralRe: Doesn't work with other objects? Pin
sharpiesharpie16-Jun-07 3:20
membersharpiesharpie16-Jun-07 3:20 
GeneralRe: Doesn't work with other objects? Pin
Brad Bruce16-Jun-07 4:33
memberBrad Bruce16-Jun-07 4:33 
GeneralRe: Doesn't work with other objects? Pin
Greg Cadmes5-Jul-07 7:19
memberGreg Cadmes5-Jul-07 7:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171114.1 | Last Updated 16 Jun 2007
Article Copyright 2007 by Manish Ranjan Kumar
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid