|
Combining Two DataSource Fields Into One DataGrid Column
By:
Scott Mitchell
|
Created: 2003-05-09
|
Last Updated: 2003-05-15
|
Printer-Friendly Version
Imagine that you have a database table called
You may want to display a list of the employees and the various attributes in a DataGrid. However, instead of having a column for both the employee's first and last name, you would like to have one DataGrid column titled "Name" that displays their last name, followed by a comma, followed by their first name.
Usually, when displaying fields from the We can apply one of the following two techniques to surmount this problem:
Option 1: Adjusting the SQL Statement In SQL, you can concatenate two string ( varchar, char, etc.) fields using the +
operator. You can give a name to a column by using the AS keyword. So, to create (and name) a column that contains
the employee's last name, followed by a comma, followed by their first name, we can use the following SQL statement:
This new field is called
Option 2: Using a TemplateColumn
Now, to display the last name, followed by a comma, followed by the first name, we simply add a TemplateColumn and specify the output we're after in the ItemTemplate like so:
That's all there is to it! |
Home | FAQs | Articles | About | My Blog | Buy the Book!
Copyright 2006, Scott Mitchell. All Rights Reserved.