Formatting GridView

N

nice.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?
 
M

marss

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
 
B

bpd

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
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top