3

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

3
  • In which stage of the page lifecycle are you creating the gridview? Page load, Init? Commented Mar 18, 2013 at 18:00
  • I am doing it in PageLoad Commented Mar 18, 2013 at 18:23
  • Try moving that code to the Page_Init method. Dynamic controls should not be created on the page load, because of the page lifecycle. Commented Mar 18, 2013 at 23:34

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

2 Comments

Can you elaborate where it needs to be? I have a Site.Css page and I was under the impression I can put it anywhere in there. Thanks
If you have started from New Web Site template and using the Site master page, then CSS classes you put in site.css should be available to any page.
1

The best way is to assign CssClass, as you are doing. And then control style using selectors like this:

Codehind

MyGridView.CssClass = "aclass";

CSS

.aclass{Border: 1px solid lighgray;}
.aclass tr td {background:green; font-weight:bold; color: white}

Gridview is rendered as normal table in HTML, so you can do this on CSS side to reduce code behind load.

Comments

1

Bootstrap class can also be added in code behind like

protected void gvEmpContactsHistory_SelectedIndexChanged(object sender, EventArgs e)
    {
        string val = Convert.ToDateTime(gvEmpContactsHistory.SelectedDataKey.Value).ToString("dd-MM-yyyy hh:mm:ss", new System.Globalization.CultureInfo("en-US"));
        GetEmployeeDetail(val);

        foreach (GridViewRow row in gvEmpContactsHistory.Rows)
        {
            if (row.RowIndex == gvEmpContactsHistory.SelectedIndex)
            {
                row.ToolTip = string.Empty;
                row.Attributes.Add("class", "btn-success");
            }
            else
            {
                row.ToolTip = "Click to select this row.";
                row.Attributes.Add("class", "btn-outline-success");
            }
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.