This is a migrated thread and some comments may be shown as answers.

change Telerik RadGridView cell color depending on another cell value

3 Answers 2908 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Danilo
Top achievements
Rank 1
Danilo asked on 31 Oct 2014, 08:14 AM
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? :)

3 Answers, 1 is accepted

Sort by
0
Accepted
Stefan
Telerik team
answered on 31 Oct 2014, 08:56 AM
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.

 
0
Danilo
Top achievements
Rank 1
answered on 31 Oct 2014, 09:14 AM
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
0
Stefan
Telerik team
answered on 31 Oct 2014, 10:59 AM
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.

 
Tags
GridView
Asked by
Danilo
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Danilo
Top achievements
Rank 1
Share this question
or