Adding CSS to GridView in Code Behind
asp.net, c#, css, gridview
Solution
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[1].CssClass = "controlbackcolor";
e.Row.Cells[3].CssClass = "controlbackcolor";
}
}
this should work but you need to make sure that the css classes are put in the right place or linked.. otherwise you wont get the style applied
Your Css class declaration should look something like the following in head tag or link:
.controlbackcolor { background: green; font-weight: bold; color: White; }
Problem
I am trying to style my GridView object but I can't seem to get it to use the CSS class. I am creating the GridViews dynamically, so they are all created in code-behind. I have tried the following and nothing seems to work. ``` for (...) { GridView gv = new GridView(); gv.CssClass = "aclass"; gv.Attributes.Add("class", "aclass"); } ``` and also in the RowDataBound event: ``` foreach (row in gv) e.Row.Cells[i].CssClass = "aClass"; ``` and yet I still cannot style my data. Any advice is greatly appreciated