Click here to Skip to main content
13,196,946 members (58,103 online)
Click here to Skip to main content
Add your own
alternative version


22 bookmarked
Posted 13 Jul 2003

Displaying Composite Text in DropDown

, 13 Jul 2003
Rate this:
Please Sign up or sign in to vote.
How can you display more then one field in text of the dropdown? Like if you have to display text, which is actually a combination of two fields in the data table. Here is a small tutorial on that.


VB.NET eases the job of a programmer to bind data to the dropdown. It is simply a matter of four lines to bind data, You just have to assign some properties of the dropdown and you are done. No loops, nothing!

UserDropDown.DataTextField = "FirstName"
UserDropDown.DataValueField = "UserID"
UserDropDown.DataSource = UserDataSet.Tables(0).DefaultView

As you all know, it’s a very easy approach to display information in the dropdown. But how about when you have to display more then one field in the text of the dropdown? Like you have to display text which is actually a combination of two fields in the data table.

Creating Composite Text

Let’s take a scenario of the user information system. You have a dropdown in which you want to populate a list of all users. And you want display last name and first name separated by the space (or coma) in the text of the dropdown. How will you do that? Looping to each row of the table and adding to the list item to the dropdown?

Well there is another cleaner ‘way’ to solve this requirement. You just have to add a dynamic data column (with format you want) to the table. And that’s all. Look at the following code sniplet

Dim DynColumn As New DataColumn()
With DynColumn
 .ColumnName = "FullName"
 .DataType = System.Type.GetType("System.String")
 .Expression = "LastName+' '+FirstName"
End With

UserDropDown.DataTextField = "FullName "
UserDropDown.DataValueField = "UserID"
UserDropDown.DataSource = UserDataSet.Tables(0).DefaultView

Please note that I have set "FullName" column to the DataTextField property of the DropDown instead of other fields of the table. Above code will simply populate drop down with composite text, which is concatenation of field's last name and first name.

If you can notice the last property is set to the dynamic column object. It’s an Expression property of the DataColumn. It will automatically populate the values of the column values based on the expression you have set for that column.

Search your MSDN for "DataColumn.Expression Property" for more explanation about the Expression property. 

Anyway you must have realized the power of the Expression property after reading above article. Using this technique you can easily and dynamically create DataColumn with complex combinations of the data fields.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Firoz Ansari
Web Developer
United States United States
Firoz has been developing Microsoft solutions for the last 5 years. He commence in the world of .NET with VS.NET Beta 2 and astonied with this technology. Right now, he is working as a Sr. Software Engineer in Pune (India), fully engaged with the web based application development using .NET Framework. His main interests includes Web/Window based application development using 3/N-Tier Architecture, Object Oriented Analysis And Design, UML, Design Patterns and RUP. Having M.Sc. (Computer Science) as Professional Qualification, he is also Microsoft Cerified Application Developer (for Microsoft .NET). He may be reached at

You may also be interested in...

Comments and Discussions

GeneralNice one...It works... Pin
paramg1235-Dec-08 0:21
memberparamg1235-Dec-08 0:21 
GeneralDropDown list control uses Pin
painuli_ramesh22-Mar-07 2:54
memberpainuli_ramesh22-Mar-07 2:54 
QuestionWhat if one of the fields is null? Pin
jornj7922-Feb-06 2:54
memberjornj7922-Feb-06 2:54 
AnswerRe: What if one of the fields is null? Pin
robmays20-Sep-06 9:49
memberrobmays20-Sep-06 9:49 
GeneralPoor Pin
Diego Mijelshon14-Jul-03 2:59
memberDiego Mijelshon14-Jul-03 2:59 
IMHO, an article should explain a little more than something that can be easily found out by reading the help.

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.171019.1 | Last Updated 14 Jul 2003
Article Copyright 2003 by Firoz Ansari
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid