We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Changing color of the cell based on its value

Hello,

I want to change the color of a cell, based on its value. I want to check a condition on the column whether its value is greater or less than the current date? Is it possible? Kindly help me do it.

3 Replies

VN Vignesh Natarajan Syncfusion Team November 28, 2019 10:30 AM UTC

Hi Junaid,  

Thanks for contacting Syncfusion forums.  

Query: “I want to check a condition on the column whether its value is greater or less than the current date? Is it possible? 
 
Yes. We can achieve your requirement using QueryCellInfo event and AddClass() method of EjsGrid. QueryCellInfo event will be triggered when each and every cell is created. Using AddClass method, you can customize the cell using CSS styles.  

Refer the below code example.  

<EjsGrid DataSource="@Orders" AllowFiltering="true" Height="315"> 
    <GridFilterSettings Type="FilterType.Menu"></GridFilterSettings> 
    <GridEvents QueryCellInfo="CellInfoHandler" TValue="Order"></GridEvents> 
    <GridColumns> 
. . . . . . . . .. . . . . . 
    </GridColumns> 
</EjsGrid> 
<style> 
    .currentweek{ 
        background-color: green; 
    } 
    .previousweek{ 
        background-color:yellow; 
    } 
    .previousmonth{ 
        background-color: red; 
    } 
</style> 
@code{ 
    public List<Order> Orders { get; set; } 
    public void CellInfoHandler(QueryCellInfoEventArgs<Order> Args) 
    { 
        if (Args.Column.Field == "OrderDate") { 
            if ( Args.Data.OrderDate < DateTime.Now.AddDays(-20)) 
            { 
                Args.Cell.AddClass(new string[] { "previousmonth" }); 
            } 
            else if (Args.Data.OrderDate < DateTime.Now.AddDays(-10)) 
            { 
                Args.Cell.AddClass(new string[] { "previousweek" }); 
            } 
            else 
            { 
                Args.Cell.AddClass(new string[] { "currentweek" }); 
            } 
        } 
    } 

Refer our UG documentation for your reference 


Please get back to us if you have further queries.  

Regards, 
Vignesh Natarajan. 



JU Junaid November 28, 2019 11:56 AM UTC

Thanks. I solved this problem with the following code:

          public void QueryCellInfoHandler(QueryCellInfoEventArgs<Order> args)
            {
                var diff = Convert.ToDateTime(args.Data.DeliveryDate.Value.ToString("yyyy-MM-dd")).Subtract(DateTime.Now);
                var weeks = (int)diff.Days / 7;

                if (args.Column.Field == "DeliveryDate")
                {
                    if (weeks > 2 && weeks <= 4)
                    {
                        args.Cell.SetAttribute("style", "background-color:#ffe680;text-align:center;");
                    }
                    else if (weeks <= 2)
                    {
                        args.Cell.SetAttribute("style", "background-color:#ffb3b3;text-align:center;");
                    }
                }
            }

Regards.
Junaid


VN Vignesh Natarajan Syncfusion Team November 29, 2019 03:47 AM UTC

Hi Junaid,  

Thanks for the update.  

We are glad to hear that your query has been resolved. 

Please get back to us if you have further queries. 

Regards, 
Vignesh Natarajan.

Loader.
Up arrow icon