How to change the color of a column based on the value

1 Answer 1350 Views
Grid
Salma
Top achievements
Rank 1
Iron
Salma asked on 28 Nov 2021, 09:21 AM

I am using mvc and using the server as a datasource.  I am trying to change the color of the column based on the value  . I tried to many finding on the net but none is working see at least two of them in the code below.   

 

//This example doesn't do anything.

columns.Bound(p => p.balance).Width(120).Sortable(false).Template(@<text>
                                                                                       @if (item.balance< 0)
                                                                                       {
                                                                                           <div style="background-color: Red;">
                                                                                               @item.balance;

                                                                                           </div>
                                                                                       }
                                                                                       else
                                                                                       {
                                                                                           <div style="background-color: Green;">
                                                                                               @item.amount_residence
                                                                                           </div>
                                                                                       }
                                                                                    </text>);

       //This below example throw an error

@(Html.Kendo().Grid(Model)
    .Name("grid").CellAction(cell =>
    {
        if (cell.Column.Title.Equals("balance"))
        {
            if (cell.DataItem.balance != null && cell.DataItem.balance.Value < 0)
            {
                cell.HtmlAttributes["style"] = "background-color: red";
            }
        }
    })
    .Columns...

1 Answer, 1 is accepted

Sort by
0
Ivan Danchev
Telerik team
answered on 01 Dec 2021, 06:38 PM

Hello,

The approach that uses the Template option, works as expected at my end. Here's an example:

The Index view and the Grid's declaration:

@model IEnumerable<TelerikMvcApp23.Models.OrderViewModel>

@(Html.Kendo().Grid(Model)
	.Name("Grid1")
	.Columns(columns =>
	{
		columns.Bound(p => p.OrderID);
		columns.Bound(p => p.Freight);
		columns.Bound(p => p.balance).Width(120).Sortable(false).Template(@<text>
			@if (item.balance < 0)
			{
				<div style="background-color: Red;">
					@item.balance;

				</div>
			}
			else
			{
				<div style="background-color: Green;">
					@item.amount_residence
				</div>
			}
			/**/
		</text>);
	})
	.Pageable()
	.Sortable()
	.Filterable()
	.Groupable()
)

The Model:

public class OrderViewModel
{
    public int OrderID
    {
        get;
        set;
    }

    public int balance { get; set; }

    public int amount_residence { get; set; }

    public decimal? Freight
    {
        get;
        set;
    }
}

The Index action that passes the model to the Index view:

public ActionResult Index()
{
    List<OrderViewModel> data = new List<OrderViewModel>()
    {
        new OrderViewModel
        {
            OrderID = 1,
            Freight = 100,
            balance = 1,
            amount_residence = 1000
        },
        new OrderViewModel
        {
            OrderID = 2,
            Freight = 50,
            balance = -2,
            amount_residence = 500
        }
    };

    return View(data);
}

As you can see in the attached screenshot the Template is used by the Grid.

I would advice checking the data you pass back to the client. Ensure that the fields have the proper casing. For example you've posted both the "balance" and the "amount_residence" fields starting with lower case, so that's how I've declared them in the Model I posted above.

Regards,
Ivan Danchev
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Salma
Top achievements
Rank 1
Iron
commented on 02 Dec 2021, 09:27 AM

Thanks for your reply. Bt for some reason it is not working on my end. 

 

Ivan Danchev
Telerik team
commented on 03 Dec 2021, 03:10 PM

I am not sure what is different in your end, so could you post your Grid declaration, ViewModel, and some sample data returned by the action, like in my previous post? I will give it a try and check what could be causing the difference in behavior.
Tags
Grid
Asked by
Salma
Top achievements
Rank 1
Iron
Answers by
Ivan Danchev
Telerik team
Share this question
or