change Telerik RadGridView cell color depending on another cell value

4 posts, 1 answers
  1. Danilo
    Danilo avatar
    47 posts
    Member since:
    Sep 2012

    Posted 31 Oct 2014 Link to this post

    Hello everybody

    I'm using Telerik RadGridView. I'm trying to change the color of one cell depending on the value of another cell. I'm trying to achieve this using this code:

    if (e.Column.Name == "colDate" && !string.IsNullOrEmpty(e.CellElement.Value.ToString()))
    {
        if (DateTime.Now > DateTime.Parse(e.CellElement.Value.ToString()))
        {
            e.Row.Cells["colColor"].Style.DrawFill = true;
            e.Row.Cells["colColor"].Style.BackColor = Color.Red;
            e.Row.Cells["colColor"].Style.NumberOfColors = 1;
        }
    }

    But for some reason it doesn't change the color. What I noticed is that when i replace

    "e.Row.Cells["colColor"].Style"

    with

    "e.CellElement"

    it changes the color. But I don't want to change the color of the current cell. I want it to change the color of another cell.

    Any suggestions? :)
  2. Answer
    Stefan
    Admin
    Stefan avatar
    2908 posts

    Posted 31 Oct 2014 Link to this post

    Hi Danilo,

    Thank you for writing.

    The easiest way to do this is to use the built-in conditional formatting functionality: http://www.telerik.com/help/winforms/gridview-cells-conditional-formatting-cells.html.

    Alternatively, if you want to use the CellFormatting event, in it you should format the cell, which the event is triggered for (e.CellElement) and check the other cells for your condition, instead of checking the value of the cell the event is triggered for and styling another cell. Here is how your code should look like:
    void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
    {
        if (e.Column.Name == "colColor"
            && !string.IsNullOrEmpty(e.Row.Cells["colDate"].Value.ToString())
            && DateTime.Now > DateTime.Parse(e.Row.Cells["colDate"].Value.ToString()))
        {
            e.CellElement.DrawFill = true;
            e.CellElement.BackColor = Color.Red;
            e.CellElement.NumberOfColors = 1;
        }
        else
        {
            e.CellElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
            e.CellElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
            e.CellElement.ResetValue(LightVisualElement.NumberOfColorsProperty, ValueResetFlags.Local);
        }
    }

    Note that all introduced settings should be reset accordingly. More information about the formatting events can be found here: http://www.telerik.com/help/winforms/gridview-cells-formatting-cells.html.

    I hope that you find this information useful. Should you have any other questions, do not hesitate to contact us.

    Regards,
    Stefan
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. Danilo
    Danilo avatar
    47 posts
    Member since:
    Sep 2012

    Posted 31 Oct 2014 in reply to Stefan Link to this post

    Hi Stefan

    Thanks for your answer. I didn't even thought about doing it this way... Sounds logical.
    Thank you very much. You solved one problem more :)

    Regards,
    Danilo
  4. Stefan
    Admin
    Stefan avatar
    2908 posts

    Posted 31 Oct 2014 Link to this post

    I am glad I could help Danilo. 

    Let us know should you have any other questions.

    Regards,
    Stefan
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top