Datagrid - set row colour in code

  • Thread starter Thread starter Phil Johnson
  • Start date Start date
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
 
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
 
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
 
Back
Top