This is a migrated thread and some comments may be shown as answers.

[Solved] MVC Grid - css style lost when DataBinding method is used

2 Answers 60 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Gary
Top achievements
Rank 1
Gary asked on 11 Apr 2012, 12:53 AM
Dear support team,

the code below is to set the background color of a column using CellAction function. it works fine without DataBinding, but the style will get lost on paging and sorting when DataBinding is inserted. Any idea? Thanks.

@{
 
    //set consts
    const string colNameStyled = "ColName";
     
    //render grid
    Html.Telerik().Grid(Model).Name("Grid")
        .Columns(columns =>
        {
            ...
            columns.Bound(...).Title(colNameStyled)...;
            ...
        })
        .DataBinding(dataBinding =>
        {
            dataBinding.Server().Select("Index", ...);
            dataBinding.Ajax().Select("_AjaxIndex", ...);
        })
        .CellAction(cell =>
        {
            if (cell.Column.Title == colNameStyled)
            {
                cell.HtmlAttributes["style"] = "background-color: green;";
            }
        })
    .Sortable()
        .Pageable()
        .Render();
}

2 Answers, 1 is accepted

Sort by
0
Brenda
Top achievements
Rank 1
answered on 27 Apr 2012, 11:11 AM
I am also having this problem. What is the solution?
0
Petur Subev
Telerik team
answered on 15 May 2012, 08:58 AM
Hello,

Generally speaking the CellAction is executed only in Server Binding or when initially loading (by passing a collection to the Grid constructor like in your case).
I would suggest you to set your background directly as an html attribute.
e.g.
columns.Bound(o => o.Freight).Width(220).HtmlAttributes(new { style = "background:green;"});
or you can assign a CSS class to the cell like this:
columns.Bound(o => o.Freight).Width(220).HtmlAttributes(new { @class = "greenClass"});


Kind regards,
Petur Subev
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now.
Tags
General Discussions
Asked by
Gary
Top achievements
Rank 1
Answers by
Brenda
Top achievements
Rank 1
Petur Subev
Telerik team
Share this question
or