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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Changing color of the cell based on its value

Thread ID:

Created:

Updated:

Platform:

Replies:

149489 Nov 27,2019 02:18 PM UTC Nov 29,2019 03:47 AM UTC Blazor 3
loading
Tags: Grid
Junaid
Asked On November 27, 2019 02:18 PM UTC

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.

Vignesh Natarajan [Syncfusion]
Replied On 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. 


Junaid
Replied On 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

Vignesh Natarajan [Syncfusion]
Replied On 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.

CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon