Click here to Skip to main content
Click here to Skip to main content

Displaying Composite Text in DropDown

By , 13 Jul 2003
Rate this:
Please Sign up or sign in to vote.

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!

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

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

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

Comments and Discussions

 
GeneralNice one...It works... Pinmemberparamg1235-Dec-08 0:21 
GeneralDropDown list control uses Pinmemberpainuli_ramesh22-Mar-07 2:54 
QuestionWhat if one of the fields is null? Pinmemberjornj7922-Feb-06 2:54 
AnswerRe: What if one of the fields is null? Pinmemberrobmays20-Sep-06 9:49 
GeneralPoor PinmemberDiego Mijelshon14-Jul-03 2:59 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 14 Jul 2003
Article Copyright 2003 by Firoz Ansari
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid