ASP.NET Data Web Controls
Kick Start

Resources

Newsletter

About

FAQs
 
ASP.NET Data Web Controls Kick Start
Buy the Book!

5 Stars at Amazon.com!
5-Star Rating at Amazon.com!
Amazon.com ratings updated weekly...

Join WebHost4Life.com

Advertisements
Customizing the Appearance of a DataGrid Column Value
By: Scott Mitchell | Created: 2003-05-08 | Last Updated: 2003-05-15 | Printer-Friendly Version

To follow along with the book, refer to: Chapter 3, Customizing the HTML Output



For More Information...
For more information on this FAQ, check out the following resources:

One of the many benefits of the DataGrid is the ease with which you can have the contents of a database table displayed. For example, to display the contents of a database column, you can simply use a BoundColumn with its DataField property set to the name of the database field whose value you want to appear in the column.

However, what if you find that the value in the database field is not the precise value you want displayed? For example, imagine that you wanted to display a database table named Employees that had the following fields:

  • Name - the employee's name
  • Salary - the employee's salary
  • Gender - the employee's gender

Perhaps the value of the Gender field was either an M for male, or F for female. Rather than display "M" or "F" in the DataGrid, you want to display "Male" or "Female." To accomplish this we will use a TemplateColumn whose ItemTemplate calls a DataGrid helper function.

A DataGrid helper function is a function that is used in a TemplateColumn and takes in an arbitrary number of parameters and returns a string. The string returned by the function is what is displayed in the TemplateColumn. For our example, our helper function will take in one parameter - the gener of the employee - and return a string: either "Male" or "Female", depending upon the value passed in.

First, let's create our helper function, and then we'll look at how to have it called from the DataGrid's TemplateColumn. The following function accepts a string input and returns the appropriate gender string:

Public Function PrintGender(ByVal gender As String) As String Select Case gender Case "M": Return "Male" Case "F": Return "Female" Case Else: Return "Unknown!" End Select End Function
VB.NET

 

public string PrintGender(object gender) { string myGender = (string) gender; switch(myGender) { case "M": return "Male"; case "F": return "Female"; default: return "Unknown!"; } }
C#

 

Examining the C# Version of PrintGender()...
You may be wondering why, in the C# example, the gender input parameter is of type object, instead of type string. This is because when calling the PrintGender, the results of Container.DataItem("Gender") will be passed in. This returns an object. VB.NET, by default, will automatically convert the object being inputted into a string. C#, however, will not do this, so we must accept an object as the parameter, and then cast it to a string.

Now, we need to see how to call this DataGrid helper function from our TemplateColumn. Essentially, you have to use DataBinding syntax to call the function, passing in the value of the DataSource's Gender field. Our DataGrid's declaration would look something like:

<asp:DataGrid runat="server" AutoGenerateColumns="False" ...> <Columns> ... <asp:TemplateColumn HeaderText="Gender"> <ItemTemplate> <%# PrintGender(Container.DataItem("Gender")) %> </ItemTemplate> </asp:TemplateColumn> ... </Columns> </asp:DataGrid>
VB.NET

 

That's all there is to it!

<shameless plug>
Curious as to how you can create a DataGrid helper function that can display different messages based on whether the field value is NULL? For information on this, check out the "Handling NULL Database Values" section in Chapter 3. (Chapter 3 is available online for free!)
</shameless plug>


Home | FAQs | Articles | About | My Blog | Buy the Book!

Copyright 2006, Scott Mitchell. All Rights Reserved.