How to change the background of a cell dynamically in a Kendo MVC UI Grid?

Bart Schelkens

I need to change the backgroundcolor of a kendo grid. My code looks as follows :

@(Html.Kendo().Grid<TegelCheckerModel>()
            .Name("Grid")
            .CellAction(cell =>
            {
                if (cell.Column.Title.Equals("TegelZones"))
                {
                    if (cell.DataItem.TegelZones.Contains("Extern"))
                    {
                        cell.HtmlAttributes["style"] = "background-color: red";
                    }
                }
            })
            .Columns(columns =>
            {
                columns.Bound(p => p.TegelNaam);
                columns.Bound(p => p.TegelActief).HeaderHtmlAttributes(new { style = "background: #800000;" }).ClientTemplate("#if(TegelActief){# ja #}else{# nee #}#");
                columns.Bound(p => p.TegelZones).HeaderHtmlAttributes(new { style = "background: #800000;" }); })

            .AutoBind(true)
            .Pageable()
            .Sortable()
            .Filterable()
            .DataSource(dataSource => dataSource
            .Ajax() //Or .Server()
            .Read(read => read.Action("GetTegels", "TegelChecker")
            .Data("getAlvNummerAndVoorWie"))
            )
        )

This code doesn't change the background-color of the cell. What am I missing?

I succeeded partly. This means I was able to color some part of the cell using the following code :

if (tegelTitel == "TegelActie") {
    if ($("#tegelcheckeroverzicht_klantControl_Klantddl").klantenControl().getKlant().alvNummer == "") {
        html = kendo.format("<span>" + data.TegelActie + "</span>");
    }
    else {
        if (data.TegelActieAllowed) {
            if (data.TegelActie == null) {
                html = kendo.format("<span></span>");
            }
            else {
                html = kendo.format("<span>" + data.TegelActie + "</span>");
            }
        }
        else {
            html = kendo.format("<span  STYLE='background: red; color: white;font-weight:bold'>" + data.TegelActie + "</span>");
        }
    }
}

But that means that only the background of the span is colored and not the entire cell.

Bart Schelkens

Ok I found a solution :

function gridDataBound() {
var data = this._data;
for (var x = 0; x < data.length; x++) {
    var dataItem = data[x];
    var tr = $("#Grid").find("[data-uid='" + dataItem.uid + "']");
    if (dataItem.Selected) {
        $("input:first", tr).attr("checked", "checked");
    }


    var alvNummer = $("#tegelcheckeroverzicht_klantControl_Klantddl").klantenControl().getKlant().alvNummer;
    if (alvNummer == "")
    { }
    else
    {
        var cell = $("td:nth-child(6)", tr); // Zone
        if (dataItem.HasCorrectZone) {

        }
        else {
            cell.addClass("myerror");
        }
        cell = $("td:nth-child(7)", tr); //Actie
        if (!dataItem.TegelActieAllowed) {
            cell.addClass("myerror");
        }
        cell = $("td:nth-child(8)", tr); //Rollen
        if (!dataItem.RolcodeVoldaan) {
            cell.addClass("myerror");
        }

    }
}

}

And in the grid I added :

.Events(e => e.DataBound("gridDataBound "))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to check and change color of cell value in kendo ui grid

From Dev

How to change the default filter operator in kendo ui grid mvc

From Dev

How to change the default filter operator in kendo ui grid mvc

From Dev

kendo ui grid background change event

From Dev

kendo ui grid background change event

From Dev

Kendo UI, How to manually call validate() on kendo grid cell

From Dev

Kendo UI, How to manually call validate() on kendo grid cell

From Dev

How to custom tooltip such as contains some button in grid's cell tooltip in Kendo UI MVC?

From Dev

How to custom tooltip such as contains some button in grid's cell tooltip in Kendo UI MVC?

From Dev

Kendo UI Grid cell coloring in MVC4

From Dev

How to dynamically change kendo UI treeview datasource

From Dev

How to show ellipsis for long cell values in kendo ui grid?

From Dev

How to conditionally format Kendo UI grid cell (depending on values) razor

From Dev

Sorting on Kendo UI MVC Grid

From Dev

Sorting on Kendo UI MVC Grid

From Dev

Kendo UI MVC Grid Not Updating

From Dev

How to customize the background color of selected of a Kendo UI Grid row

From Dev

Change the class of a kendo ui cell

From Dev

Change a column in Kendo UI Grid

From Dev

How to reuse and parameter a Kendo UI grid EditorTemplate (using ASP MVC)?

From Dev

How to filter a Kendo UI MVC grid using a dropdown list

From Dev

How to transpose rows and columns in a Kendo UI grid in MVC application?

From Dev

How to find the specific word in kendo UI grid in MVC

From Dev

Dynamically change the background of a cell in a ListView

From Dev

How to change the default value of Grid page size in Kendo UI?

From Dev

How to change column title dynamically in Kendo UI TreeList?

From Dev

Kendo UI grid filter cell that contains an array

From Dev

Kendo UI grid conditionally editable cell

From Dev

Kendo UI grid filter cell that contains an array

Related Related

  1. 1

    how to check and change color of cell value in kendo ui grid

  2. 2

    How to change the default filter operator in kendo ui grid mvc

  3. 3

    How to change the default filter operator in kendo ui grid mvc

  4. 4

    kendo ui grid background change event

  5. 5

    kendo ui grid background change event

  6. 6

    Kendo UI, How to manually call validate() on kendo grid cell

  7. 7

    Kendo UI, How to manually call validate() on kendo grid cell

  8. 8

    How to custom tooltip such as contains some button in grid's cell tooltip in Kendo UI MVC?

  9. 9

    How to custom tooltip such as contains some button in grid's cell tooltip in Kendo UI MVC?

  10. 10

    Kendo UI Grid cell coloring in MVC4

  11. 11

    How to dynamically change kendo UI treeview datasource

  12. 12

    How to show ellipsis for long cell values in kendo ui grid?

  13. 13

    How to conditionally format Kendo UI grid cell (depending on values) razor

  14. 14

    Sorting on Kendo UI MVC Grid

  15. 15

    Sorting on Kendo UI MVC Grid

  16. 16

    Kendo UI MVC Grid Not Updating

  17. 17

    How to customize the background color of selected of a Kendo UI Grid row

  18. 18

    Change the class of a kendo ui cell

  19. 19

    Change a column in Kendo UI Grid

  20. 20

    How to reuse and parameter a Kendo UI grid EditorTemplate (using ASP MVC)?

  21. 21

    How to filter a Kendo UI MVC grid using a dropdown list

  22. 22

    How to transpose rows and columns in a Kendo UI grid in MVC application?

  23. 23

    How to find the specific word in kendo UI grid in MVC

  24. 24

    Dynamically change the background of a cell in a ListView

  25. 25

    How to change the default value of Grid page size in Kendo UI?

  26. 26

    How to change column title dynamically in Kendo UI TreeList?

  27. 27

    Kendo UI grid filter cell that contains an array

  28. 28

    Kendo UI grid conditionally editable cell

  29. 29

    Kendo UI grid filter cell that contains an array

HotTag

Archive