470,811 Members | 1,130 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,811 developers. It's quick & easy.

Conditionally Change a DataGrid Column Value...

There are times when you will need to highlight or otherwise modify
the contents of a particular DataGrid row-column value based upon the
value in the column. In this example we will select the CompanyName,
ContactName, and ContactTitle columns from the Customers table in the
Northwind database. Whenever the value of ContactTitle equals "Owner"
we will change the font color to red.

We can do this by using an ItemTemplate where we have complete control
over the presentation of the data. We use BoundColumns for the first
two columns and an ItemTemplate for the ContactTitle column. Within
the ItemTemplate we call a function named "ChangeColors" and pass it
the value of the column. This is what the line <%#
ChangeColor(Container.DataItem("ContactTitle")) %> does. We are
getting the value of ContactTitle and passing it to the ChangeColor
function which appears in our code-behind page.

<%@ Page Language="vb"
Codebehind="CondDataGrid.aspx.vb" Inherits="CondDataGrid"
AutoEventWireup="false" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<meta content="Microsoft Visual Studio.NET 7.0" name=GENERATOR>
<meta content="Visual Basic 7.0" name=CODE_LANGUAGE>
<meta content=JavaScript name=vs_defaultClientScript>
<meta content=http://schemas.microsoft.com/intellisense/ie5
<body MS_POSITIONING="GridLayout">
<form id=Form1 method=post runat="server">
<asp:DataGrid ID="dataGrid"
AutoGenerateColumns = "False"
<asp:BoundColumn HeaderText="Company Name" DataField="CompanyName"
<asp:BoundColumn HeaderText="Contact Name" DataField="ContactName"

<asp:TemplateColumn HeaderText="Contact Title">
<%# ChangeColor(Container.DataItem("ContactTitle")) %>
Most of the code-behind file is the usual data access code to get our
resultset. At the bottom of the file, color-coded in blue, is the
ChangeColor function. As you can see, all it is doing is checking for
"Owner" in the value passed in. If the value is "Owner" then we put
font tags around the value and send it back. Otherwise, if it was not
"owner", we just send the value back to the caller to be rendered as

Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Public Class CondDataGrid
Inherits System.Web.UI.Page

Protected WithEvents dataGrid As System.Web.UI.WebControls.DataGrid

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim sqlConn As SqlConnection
Dim sqlCmd As SqlCommand

sqlConn = New SqlConnection(ConfigurationSettings.AppSettings("C onnectionString"))
sqlCmd = New SqlCommand("SELECT TOP 10 * FROM Customers",
dataGrid.DataSource = sqlCmd.ExecuteReader()
Catch ex As Exception
Response.Write(ex.ToString() & "<br>")
End Try
End Sub

Function ChangeColor(value)
If value = "Owner" Then
ChangeColor="<font color='red'>" & value & "</font>"
ChangeColor = value
End If
End Function

End Class
I hope you have seen how easy it is to modify the appearance of a
DataGrid colum based upon its value. You can, of course, base your
modifications on the value of any column you want.

AMBER [MCSD.NET MCAD.NET] http://www.dedicatedsolutions.co.uk
Nov 17 '05 #1
0 2435

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Dan Hartshorn | last post: by
2 posts views Thread by Olav Tollefsen | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.