472,978 Members | 2,425 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,978 software developers and data experts.

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 2587

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: PeterZ | last post by:
G'day, After doing much searching and pinching bits of ideas from here there and everywhere I came up with a fairly 'clean' solution of including a comboBox into a dataGrid column. You can...
by: Amber | last post by:
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...
by: Dan Hartshorn | last post by:
VS.NET 2003, C#, Windows Server 2003. I have a datagrid and I want the last column to be either an EditCommandColumn or a template column, depending on a value I have. The value changes for each...
by: Olav Tollefsen | last post by:
I'm creating an ASP.NET e-commerce web site and I have to conditionally (depending on site / user settings) display prices either excluding or including tax. Prices are typically read from a...
by: Rob Kroese | last post by:
I've got a form with a datagrid that displays a list of items, along with several textboxes, comboboxes, etc., that display the details for the selected item. The columns in the datagrid and the...
by: Roger | last post by:
I have a datagrid and would like to know what even fires when a cell is changed? I want to know when the user changes a cell and moves to the next. I have some code that needs to be done to...
by: simon | last post by:
hello, new to vb.net, have a few questions about DataGrid. I have a dataGrid that is working pulling a dataset back from a stored proc and binding to the datagrid for display the datagrid's...
by: Billy | last post by:
Change DataGrid EditControl On Data Value Hi, I have a datagrid, and on editing, I want to change the control in the third colunm based on the value of the first column. The value in the...
by: msrahul | last post by:
HI ALL Plz Help Me. I have a datagrid with a few colums. One column is conaining 1's or 0's. Its like a flag column. It either has 0 or 1. Now what i need is that if any row has 1 in this column...
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.