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

17 posts, 1 answers
  1. LesPinter
    LesPinter avatar
    3 posts
    Member since:
    Jul 2007

    Posted 17 Jun 2010 Link to this post

    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
  2. Answer
    Tsvyatko
    Admin
    Tsvyatko avatar
    833 posts

    Posted 18 Jun 2010 Link to this post

    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
  3. UI for WPF is Visual Studio 2017 Ready
  4. LesPinter
    LesPinter avatar
    3 posts
    Member since:
    Jul 2007

    Posted 24 Jun 2010 Link to this post

    Hi Tsvyatko,

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

    Les
  5. Hicham
    Hicham avatar
    3 posts
    Member since:
    Jul 2012

    Posted 18 Dec 2011 Link to this post

    hi,
    please, can anyone tell me how can i do the same for a row in a RadTreeListView??
    thanks.
  6. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 19 Dec 2011 Link to this post

    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 >>

  7. sandy
    sandy avatar
    23 posts
    Member since:
    Aug 2010

    Posted 05 Jan 2012 Link to this post

    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.
  8. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 06 Jan 2012 Link to this post

    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 >>

  9. Angel
    Angel avatar
    8 posts
    Member since:
    Feb 2011

    Posted 27 Jan 2012 Link to this post

    Hello,

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

    Thanks!
  10. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 27 Jan 2012 Link to this post

    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 >>

  11. David
    David avatar
    5 posts
    Member since:
    Dec 2011

    Posted 07 Feb 2012 Link to this post

    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 


  12. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 08 Feb 2012 Link to this post

    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 >>
  13. Valerie
    Valerie avatar
    36 posts
    Member since:
    Nov 2011

    Posted 06 Dec 2012 Link to this post

    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.
  14. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 06 Dec 2012 Link to this post

    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.

  15. Valerie
    Valerie avatar
    36 posts
    Member since:
    Nov 2011

    Posted 07 Dec 2012 Link to this post

    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!
  16. Claudio
    Claudio avatar
    5 posts
    Member since:
    Aug 2013

    Posted 28 Aug 2013 Link to this post

    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
  17. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 29 Aug 2013 Link to this post

    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 >>
  18. Claudio
    Claudio avatar
    5 posts
    Member since:
    Aug 2013

    Posted 03 Sep 2013 Link to this post

    Hi Maya, 
    That worked perfectly.

    Thank you
Back to Top
UI for WPF is Visual Studio 2017 Ready