Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding CSS to GridView in Code Behind

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

like image 242
Jenny54321 Avatar asked Sep 15 '26 10:09

Jenny54321


1 Answers

    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; }

like image 152
Kokulan Eswaranathan Avatar answered Sep 17 '26 00:09

Kokulan Eswaranathan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!