Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Telerik ASP.NET MVC 3 Grid - setting row background

I'm using Telerik Grid. I need to set background color for the entire row based on some property in view model. I've tried to do it as below by setting a background in IF statement for each column but backgound applies only on element not all cell (td). Also it seems it's a very "dirty" way to accomplish this task.

@(Html.Telerik().Grid(Model.SomeItems).Name("Grid")
               .Columns(columns =>
               {
                   columns.Template(
                       @<text>
                          @if (Model.IsOfSpecialColor)
                           {
                             <div class="gridRowBackground">
                               @Html.ActionLink(...)
                             </div>
                           }
                           else
                           {                                        
                              @Html.ActionLink(...)
                           }
                      </text>).Title("Blabla");
                });
like image 933
Oleksandr Kobylianskyi Avatar asked Dec 12 '22 00:12

Oleksandr Kobylianskyi


2 Answers

You can change it using onRowDataBound event

@(Html.Telerik().Grid<Customer>()
      .Name("Grid2")
      .DataBinding(d => d.Ajax().Select("Select", "Home"))
      .ClientEvents(e => e.OnRowDataBound("onRowDataBound"))
)

and the function is

<script>
function onRowDataBound(e) {
    if (e.dataItem.ID == 2) {
        e.row.style.backgroundColor = "grey";
    }
}
</script>
like image 60
Tassadaque Avatar answered Dec 29 '22 10:12

Tassadaque


If you are using server data binding, you can use CellAction. However, if you are using ajax data binding, you will need to use a solution like Tassadaque suggests.

@(Html.Telerik().Grid(Model.SomeItems)
.Name("Grid")
.CellAction(cell =>
{
    if (cell.DataItem.IsOfSpecialColor.Value)
    {
      cell.HtmlAttributes["style"] = "background-color: red";
    }
})
like image 33
Daniel Avatar answered Dec 29 '22 11:12

Daniel