Formatting GridView

  • Thread starter Thread starter nice.com
  • Start date Start date
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?
 
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
 
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
}
}
}
 
Back
Top