quick datagrid format question

G

Guest

Hi,

Is it possible to format the background of rows (or cells) in a datagrid
based on the value of the data in a particular cell. e.g. I have a datagrid
of say 21 records, 3 records per day of the week. Is it possible to place a
background color on the rows based on the day e.g. Monday records have a gray
background, Tuesday records have a blue background etc.

Thanks
Niall
 
G

Guest

Hi Niall,

You can do it in datagrid_ItemDataBound event:

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
DateTime date = Convert.ToDateTime(e.Item.Cells[date_field_index].Text);
if (date.DayOfWeek == DayOfWeek.Monday){
e.Item.BackColor = Color.Gray;
}
}

HTH

Elton Wang
(e-mail address removed)
 
G

Guest

Thanks Elton.

Why does Convert.ToString(e.Item.Cells(0).Text) return the column heading
(in my case its "Day") rather than the values e.g. "Mon", "Tues" etc

Thanks
N
 
G

Guest

When e.Item.ItemType = ListItemType.Header, it is "Day". In case
ListItemType.Item and ListItemType.AlternatingItem, it should have a value
from data source. How do you bind the data source?

Elton

NH said:
Thanks Elton.

Why does Convert.ToString(e.Item.Cells(0).Text) return the column heading
(in my case its "Day") rather than the values e.g. "Mon", "Tues" etc

Thanks
N

Elton W said:
Hi Niall,

You can do it in datagrid_ItemDataBound event:

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
DateTime date = Convert.ToDateTime(e.Item.Cells[date_field_index].Text);
if (date.DayOfWeek == DayOfWeek.Monday){
e.Item.BackColor = Color.Gray;
}
}

HTH

Elton Wang
(e-mail address removed)
 

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

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top