Click here to Skip to main content
15,881,967 members
Articles / Web Development / ASP.NET
Article

Displaying Composite Text in DropDown

Rate me:
Please Sign up or sign in to vote.
2.94/5 (15 votes)
13 Jul 20032 min read 63.8K   22   5
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.

Introduction

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!

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

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

VB.NET
Dim DynColumn As New DataColumn()
With DynColumn
 .ColumnName = "FullName"
 .DataType = System.Type.GetType("System.String")
 .Expression = "LastName+' '+FirstName"
End With
UserDataSet.Tables(0).Columns.Add(DynColumn)

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

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.

License

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


Written By
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 firoz.ansari@indiatimes.com

Comments and Discussions

 
GeneralNice one...It works... Pin
Parameshwaran G5-Dec-08 0:21
Parameshwaran G5-Dec-08 0:21 
GeneralDropDown list control uses Pin
painuli_ramesh22-Mar-07 2:54
painuli_ramesh22-Mar-07 2:54 
QuestionWhat if one of the fields is null? Pin
jornj7922-Feb-06 2:54
jornj7922-Feb-06 2:54 
AnswerRe: What if one of the fields is null? Pin
robmays20-Sep-06 9:49
robmays20-Sep-06 9:49 
GeneralPoor Pin
Diego Mijelshon14-Jul-03 2:59
Diego Mijelshon14-Jul-03 2:59 

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.