473,473 Members | 1,805 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Formatting GridView

Hi,

It it possible to alter properties of a gridview control based on the
data it holds?

Specifically, I would like to alter colours held in the data. For
example....here's the data

Surname Forename Registered RecentContact
Smith Henry no no
Jones Wesley yes no
Adams Sarah yes yes

I would like to display records in red for those unregistered.. yellow
for those registered without recent contact.. and green for those
registered with recent contact.

Can this be done simply?

Apr 11 '07 #1
4 3237
Yes. The most flexible way is to handle the RowDataBound event. In the event
you can access the values of the columns and set any visual properties as
you wish.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
<ni******@gmail.comwrote in message
news:11**********************@w1g2000hsg.googlegro ups.com...
Hi,

It it possible to alter properties of a gridview control based on the
data it holds?

Specifically, I would like to alter colours held in the data. For
example....here's the data

Surname Forename Registered RecentContact
Smith Henry no no
Jones Wesley yes no
Adams Sarah yes yes

I would like to display records in red for those unregistered.. yellow
for those registered without recent contact.. and green for those
registered with recent contact.

Can this be done simply?

Apr 11 '07 #2

ni******@gmail.com wrote:
Hi,

It it possible to alter properties of a gridview control based on the
data it holds?

Specifically, I would like to alter colours held in the data. For
example....here's the data

Surname Forename Registered RecentContact
Smith Henry no no
Jones Wesley yes no
Adams Sarah yes yes

I would like to display records in red for those unregistered.. yellow
for those registered without recent contact.. and green for those
registered with recent contact.

Can this be done simply?
In the OnRowDataBound event handler you have the all required data to
do this.
protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{.....

Here e.Row is a row of the GridView and e.Row.DataItem is an object
you bind to the GridView.
Suppose, you bind an object Person with the property Registered. To
paint row in red you need something like this:
Person p = (Person)e.Row.DataItem;
if (!p.Registered)
e.Row["background-color"] = "red";

Regards

Apr 11 '07 #3
bpd
On Apr 11, 9:58 am, nice....@gmail.com wrote:
Hi,

It it possible to alter properties of a gridview control based on the
data it holds?

Specifically, I would like to alter colours held in the data. For
example....here's the data

Surname Forename Registered RecentContact
Smith Henry no no
Jones Wesley yes no
Adams Sarah yes yes

I would like to display records in red for those unregistered.. yellow
for those registered without recent contact.. and green for those
registered with recent contact.

Can this be done simply?
Use the gridview RowDataBound event.

in C#:

<gridview id>_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//non-edit mode
if ((e.Row.RowState == DataControlRowState.Normal) ||
(e.Row.RowState == DataControlRowState.Alternate))
{
<your code to alter row color here for example:
lbl = (Label)e.Row.Cells[1].Controls[1];
(obviously, lbl is defined as a label --- Label lbl;)
lbl.ForeColor = Color.Red; (or yellow or
green) to set the text color
--or--
e.Row.Cells[1].BackColor = Color.Red; (or
yellow or green) to set the color of the cell
}
}
}

Apr 11 '07 #4

marss wrote:
e.Row["background-color"] = "red";
Excuse for the misprint. Correction:
e.Row.Style["background-color"] = "red";

Apr 12 '07 #5

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

Similar topics

0
by: Not Me | last post by:
Hi, It it possible to alter properties of a gridview control based on the data it holds? Specifically, I would like to alter colours held in the data. For example....here's the data ...
4
by: Nalaka | last post by:
Hi, I have two questions about gridViews. 1. How can I intercept the row/column values at loading to change values? 2. After I update a row (using default update functionality), how can I...
3
by: CSharpguy | last post by:
I'm trying to format the GridView on my web form and its not working. for money I have {0:C} and for date fields, I have {0:d} and the format isn't changing, what else do I need to do to get the...
0
by: msnews.microsoft.com | last post by:
Hello everyone., please help. I took the CSS from a DataGrid and applied it to the GridView. It does not take the CSS settings. Simple settings like; border: solid 1px black fails to apply...
4
by: Ken Wigle | last post by:
All, I would be very grateful for any help on this question. I have an application in asp.net 2.0 where I dynamically create a datatable and then bind that to a gridview. Unfortunately, the...
1
by: colo | last post by:
Hi! I have a problem which I think has a simple solution, but I can't figure out why it's not working. Some background... I'm populating a GridView with info from an Access database like this: ...
0
by: Adam Sandler | last post by:
Hello, My codebehind builds a GridView at runtime and shovels the data off as a part of a callback... Dim returnstring As String = String.Empty Dim gv As New GridView gv.DataSource = ds...
0
by: Don Miller | last post by:
I'd like to use a programmatically bound GridView to display a dataset (or work with a datareader) that may include everchanging numbers of database columns, in unpredictable order, with varying...
7
by: Bobby Edward | last post by:
What is the formatstring for mm/dd/yy (gridview column)? I always want it to be 6 digits. Thanks.
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.