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"
Src="/Portals/57ad7180-c5e7-49f5-b282-c6475cdb7ee7/CondDataGrid.aspx.vb"
Codebehind="CondDataGrid.aspx.vb" Inherits="CondDataGrid"
AutoEventWireup="false" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>CondDataGrid</title>
<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
name=vs_targetSchema>
</head>
<body MS_POSITIONING="GridLayout">
<form id=Form1 method=post runat="server">
<asp:DataGrid ID="dataGrid"
AutoGenerateColumns = "False"
Runat="server">
<Columns>
<asp:BoundColumn HeaderText="Company Name" DataField="CompanyName"
/>
<asp:BoundColumn HeaderText="Contact Name" DataField="ContactName"
/>

<asp:TemplateColumn HeaderText="Contact Title">
<ItemTemplate>
<%# ChangeColor(Container.DataItem("ContactTitle")) %>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</form>
</body>
</html>
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
is.

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

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

Function ChangeColor(value)
If value = "Owner" Then
ChangeColor="<font color='red'>" & value & "</font>"
Else
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

3
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...
0
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...
0
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...
2
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...
4
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...
4
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...
2
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...
2
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...
2
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...
2
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...
2
isladogs
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...
0
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...
4
NeoPa
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 :...
3
NeoPa
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...
3
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...
0
NeoPa
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...
0
isladogs
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...
4
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.