How to Display and Value Separately in Combobox/Listbox using C#

No.of Views1538
Bookmarked0 times
Downloads 
Votes0
By  mranjankumar   On  16 Feb 2010 03:02:50
Tag : CSharp , List Controls
How to Display and Value Separately in Combobox/Listbox using C#
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction


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.

 

Image Loading

Figure-01 Expectingf Result

Background


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

 

CodeGain so that a beginner user can resolve this common problem.

Image Loading

Figure-02 Samples

 

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.

Code Snippet

public class ItemObject
{
private string key;
private object valueOfKey;

/// <summary>
/// Overloaded constructor.
/// </summary>
/// <param name="key">Key of object.</param>
/// <param name="valueOfKey">Value of object.</param>
public ItemObject(string key, object valueOfKey)
{
this.key = key;
this.valueOfKey = valueOfKey;
}

/// <summary>
/// Default constructor
/// </summary>
public ItemObject()
{
key = string.Empty;
valueOfKey = string.Empty;
}

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

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

/// <summary>
/// Gets or sets Key of object.
/// </summary>
public string Key
{
get { return key; }
set { key = value; }
}

/// <summary>
/// Gets or sets Value of object.
/// </summary>
public object ValueOfKey
{
get { return valueOfKey; }
set { valueOfKey = value; }
}
}

 

Willing to Share

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. 

Project Samples

 
Download Demo files -23 kb

Download source files -23 kb

 

 
Sign Up to vote for this article
 
About Author
 
mranjankumar
Occupation-Not Provided
Company-Not Provided
Member Type-Fresh
Location-Not Provided
Joined date-28 Oct 2009
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top