Datagrid - set row colour in code

P

Phil Johnson

Does anybody know of any examples on how to set both the row colour and the
alternate row colour of a datagrid in code?

I need to do this at runtime, probably in the page load event or something
similar.
--
Regards,

Phillip Johnson (MCSD For .NET)
PJ Software Development
www.pjsoftwaredevelopment.com
 
E

Eliyahu Goldin

The custom is to do it in RowDataBound event although RowCreated or
PreRender can be used as well.

Here is an example:

protected void myGrid_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
switch (e.Row.RowState)
{
case DataControlRowState.Normal:
e.Row.BackColor = Color.Blue;
break;
case DataControlRowState.Alternate:
e.Row.BackColor = Color.Green;
break;
case DataControlRowState.Selected:
e.Row.BackColor = Color.Red;
break;
}
}
}


--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
 
P

Phil Johnson

Thanks for your help again Eliyahu, much appreciated.

--
Regards,

Phillip Johnson (MCSD For .NET)
PJ Software Development
www.pjsoftwaredevelopment.com


Eliyahu Goldin said:
The custom is to do it in RowDataBound event although RowCreated or
PreRender can be used as well.

Here is an example:

protected void myGrid_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
switch (e.Row.RowState)
{
case DataControlRowState.Normal:
e.Row.BackColor = Color.Blue;
break;
case DataControlRowState.Alternate:
e.Row.BackColor = Color.Green;
break;
case DataControlRowState.Selected:
e.Row.BackColor = Color.Red;
break;
}
}
}


--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


Phil Johnson said:
Does anybody know of any examples on how to set both the row colour and
the
alternate row colour of a datagrid in code?

I need to do this at runtime, probably in the page load event or something
similar.
--
Regards,

Phillip Johnson (MCSD For .NET)
PJ Software Development
www.pjsoftwaredevelopment.com
 

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,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top