Search code examples
cssasp.net-mvcasp.net-mvc-5webgrid

How to set custom font color of webgrid header?


I'm using webgrid in my mvc web apps with enabled sorting.

var grid = new WebGrid(
    source: Model,
    canPage: false,
    ajaxUpdateContainerId: "mainGrid"
    );

If property canSort=true(by default) then font color of header names is always blue (though I use css), but I want to change it for another.

How can i do it keeping enabled sorting?

My code:

View:

    @grid.GetHtml(
        columns: grid.Columns(
            grid.Column(header: "", columnName: "isChecked", canSort: false,
                format: @<text><input type="checkbox" name="checkbox_selected" value="@item.index" /></text>),
            grid.Column(header: "Имя", columnName: "name", style: "name"),
            grid.Column(header: "Тип", columnName: "type", style: "type"),
            grid.Column(header: "Дата изменения", columnName: "date", style: "date"),
            grid.Column(header: "Размер", columnName: "size", style: "size")
            ),

       tableStyle: "grid_table",
       headerStyle: "header_table",
       rowStyle: "row_table",
       alternatingRowStyle: "alt-row_table"
   )

CSS:

.header_table
{
background-color: limegreen;
color: black;
padding-bottom: 4px;
padding-top: 4px;
}

All other styles normally working, but .header_table color when sorting enabled not working (when disabled it works).


Solution

  • When you render your web grid in your view, specify CSS class names:

    <div>
        @grid.GetHtml(
                tableStyle: "webgrid-table",
                headerStyle: "webgrid-header",
                footerStyle: "webgrid-footer",
                alternatingRowStyle: "webgrid-alternating-row",
                selectedRowStyle: "webgrid-selected-row",
                rowStyle: "webgrid-row-style",
                mode: WebGridPagerModes.All,
                columns:
                // ... more stuff here
    </div>
    

    We can now specify the CSS styles for the grid and get rid of the blue (I've assumed that the rendered blue is because the headers contain links (<a> tags), hence the second CSS declaration:

    .webgrid-header { color: #FF0000; background-color: #00FF00; }
    .webgrid-header a { color: #FF0000; }
    

    More detailed explanation on this page: https://stick2basic.wordpress.com/2013/04/10/how-to-set-webgrid-style-in-mvc/

    I hope this helps!