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

Setting radGridView row background color based on the value of a column in the row

16 Answers 1135 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Les
Top achievements
Rank 2
Les asked on 17 Jun 2010, 04:15 PM
Hi guys,

   I'm a WPF developer, and I'm looking for some sample code that will show me how to set the radGridView row background color based on the value of a column in the row. Do you have an example that will do so?
   Specifically, I want to set a row's background color to Red if the value of Due_Date is within 1 day of today, and to Yellow if it's between 2 and 3 days from today.

Thanks,

Les

16 Answers, 1 is accepted

Sort by
0
Accepted
Tsvyatko
Telerik team
answered on 18 Jun 2010, 09:17 AM
Hi LesPinter,

I have prepared simple project demonstrating how to achieve the desired functionality using row styles. Please check the attachment and let me know if this works for you.

If you have any further assistance do not hesitate to contact us.

All the best,
Tsvyatko
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Les
Top achievements
Rank 2
answered on 24 Jun 2010, 03:50 PM
Hi Tsvyatko,

   Thanks - that's EXACTLY what I needed. You guys are AMAZING!

Les
0
Hicham
Top achievements
Rank 1
answered on 19 Dec 2011, 03:54 AM
hi,
please, can anyone tell me how can i do the same for a row in a RadTreeListView??
thanks.
0
Maya
Telerik team
answered on 19 Dec 2011, 06:53 AM
Hello Hicham,

You can create a RowStyleSelector. Please take a look at our online documentation and demos for a reference. Although the examples are for RadGridView, exactly the same approach can be used for RadTreeListView as well. 
 

All the best,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
sandy
Top achievements
Rank 1
Iron
Veteran
answered on 05 Jan 2012, 09:32 PM
hi,
Any one give solution.
am using silverlight radgridview for changing row color based on col value.
same isuue but am using silver light insted of wpf.
0
Maya
Telerik team
answered on 06 Jan 2012, 07:25 AM
Hello Sandy,

Actually, exactly the same approach is applicable for Silverlight as well. So, you can implement the same idea in your application.  

Greetings,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Angel
Top achievements
Rank 1
answered on 27 Jan 2012, 06:12 PM
Hello,

Is it possible to change only the color of a specific column?

Thanks!
0
Maya
Telerik team
answered on 27 Jan 2012, 06:24 PM
Hi Les,

You can use CellStyleSelector - for further information please refer to our online documentation and demos.  

Kind regards,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
David
Top achievements
Rank 1
answered on 07 Feb 2012, 08:28 PM
For anyone trying to make this work in silverlight:

Contrary to Maya's suggestion, this does not actually work in silverlight 4. See references below. The quickest and easiest work around is probably something like the code below. If anyone has better work-around, I would be happy to hear it.

private void ReportGrid_RowLoaded(object sender, RowLoadedEventArgs e)
{
    if (e.Row.DataContext is Report)
    {
        e.Row.Background = (e.Row.DataContext as Report).Active ? new SolidColorBrush(Colors.White) : new SolidColorBrush(Colors.Gray);
    }
}

http://stackoverflow.com/questions/6526569/silverlight-datagridrow-background-color-change-based-on-a-column-value-and-bind 
http://stackoverflow.com/questions/4878189/silverlight-how-to-use-a-binding-in-setter-for-a-style-or-an-equivalent-work-a 
http://forums.silverlight.net/t/130096.aspx 


0
Maya
Telerik team
answered on 08 Feb 2012, 07:24 AM
Hello David,

Thank you for sharing your insights with the community. Indeed, you can set the background of a row in such a way, but there is still one drawback in this approach. The thing is that RadGridView is virtualized by default, i.e. its visual elements will be recycled and reused on scrolling. That is why it is not recommended to work directly with those elements and their properties (as it is in this case - with rows and their background).
Working with style selectors will ensure that setting the background relies on bindings and will be evaluated for each row.
Nevertheless, thank you for you feedback. 

All the best,
Maya
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Valerie
Top achievements
Rank 1
answered on 06 Dec 2012, 04:19 PM
How can you do this -- identify a column that has the value that will drive the background color -- if you are using a dynamic column list for your RadGridView?
That is, in the example, the Due_Date property on the object is used for this value. What if you don't have a fixed property because they are dynamic and are loaded at runtime with calls to GridViewDataColumn.
0
Maya
Telerik team
answered on 06 Dec 2012, 04:47 PM
Hi Les,

You can try working with RowStyleSelector. Check out this article and this demo for a reference. 

Regards,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Valerie
Top achievements
Rank 1
answered on 07 Dec 2012, 08:01 PM
Thank you Maya for such a fast response! (This is Valerie, not Les)
I had been using a RowStyleSelector but not using it correctly. The demo you pointed me to showed what I was doing wrong. Thank you so much!
0
Claudio
Top achievements
Rank 1
answered on 28 Aug 2013, 08:34 PM
I'm working on a Silverlight RadGridView, and I was wondering if is possible to determine the background color but based on the combination of 2 cells values?
An example would be if cell1 - cell2< 0 then color.Red.

Thank you in advance
0
Maya
Telerik team
answered on 29 Aug 2013, 06:38 AM
Hello Claudio,

You can work with two properties values simultaneously. As it is in the case from the documentation - instead of checking only the value of StadiumCapacity, you can verify 'Name' property and only after that - return the style you want.

Regards,
Maya
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Claudio
Top achievements
Rank 1
answered on 03 Sep 2013, 06:23 PM
Hi Maya, 
That worked perfectly.

Thank you
Tags
GridView
Asked by
Les
Top achievements
Rank 2
Answers by
Tsvyatko
Telerik team
Les
Top achievements
Rank 2
Hicham
Top achievements
Rank 1
Maya
Telerik team
sandy
Top achievements
Rank 1
Iron
Veteran
Angel
Top achievements
Rank 1
David
Top achievements
Rank 1
Valerie
Top achievements
Rank 1
Claudio
Top achievements
Rank 1
Share this question
or