Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Colors inside a datagrid

Status
Not open for further replies.

nickmollberg

Programmer
Joined
Aug 2, 2004
Messages
29
Location
US
Hello all.

I have a regular datagrid, and what I would like to have happen is, if a certain value in the database (billed) is false, then the entire row shows up as red.

That way, a user can, at a glance, see both the billed and unbilled records of a database, with the items of interest showing up in red.

Also, I would like to have some coloration based upon a datetime field, but I figure I should start small.

Thanks!
 
Code:
void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
	TableCell cell;
	switch (e.Item.ItemType)
	{
		case ListItemType.Item:
		case ListItemType.AlternatingItem:
		foreach(System.Web.UI.Control control in e.Item.Controls)
		{
			cell = control as TableCell;
			if (cell.Text == "3")
			{
				 e.Item.BackColor = Color.FromName("red");
			}
		}
		break;
	}
}
from this forum I believe, if you search you should find plenty more.
hth,
Marty
 
Will this color the entire row, or just the individual cell?
I'm trying something very similar, but not having much luck.
 
Entire row.
Post what you are having problem with someone will help.
Marty
 
I'm using the below, but item.Cells[1].Text doesn't seem to pull anything from the database...
I've tried item.Cells[0].Text too, but nothing pulls in.


public void DataList1_ItemDataBound (object sender, System.Web.UI.WebControls.DataListItemEventArgs e) //Handles DataList1.ItemDataBound
{
Label Titlelabel = (System.Web.UI.WebControls.Label) e.Item.FindControl("lblETitle");
string strEmpTitle = Titlelabel.Text;
DataGrid dg = (DataGrid)(e.Item.FindControl("Datagrid1"));
dg.DataSource = getEmp(strEmpTitle);
dg.DataBind();

foreach(DataGridItem item in dg.Items)
{

Response.Write( item.Cells[1].Text );
if(item.Cells[1].Text.ToString() == "0")
{

item.BackColor = Color.White;
item.ForeColor = Color.Black;
}
else
{
item.BackColor = Color.White;
item.ForeColor = Color.Blue;
}


}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top