WPF GridView Change Cell Color Based on Value

5 posts, 0 answers
  1. Sagar
    Sagar avatar
    1 posts
    Member since:
    Dec 2013

    Posted 11 Dec 2013 Link to this post

    hi
     i have one GridView that contains Column Name "Day1", "Day2" ....to "Day31".

    Now i want to change Cell Background and ForeColor Based On Value.

    For All the 31 Columns
    if cell value = "P" then Green  Background and forecolor white.


    how can i do this.
    Please Help me.
  2. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 11 Dec 2013 Link to this post

    Hi,

    You can check our online documentation on how to use a CellStyleSelector here. The "Style Selectors -> Cells" WPF demo would also be helpful for you.

    Regards,
    Didie
    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 >>
  3. UI for WPF is Visual Studio 2017 Ready
  4. Renier Pretorius
    Renier Pretorius avatar
    46 posts
    Member since:
    Apr 2010

    Posted 01 Jun 2015 in reply to Dimitrina Link to this post

    Hi,

     I have taken a look at the documentation and the demos (Silverlight) but in all cases your CellStyleSelector is based on a business object with a specific property in the column to which the style needs to be applied. In my case my business object as an array of values which I want to display using the same CellStyleSelector. I build the columns in the code behind. So to use your method my CellStyleSelector must be aware of the specific column in question. Any pointers?

     

    Regards

    Renier

  5. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 02 Jun 2015 Link to this post

    Hi Renier,

    You can find the column associated with the cell passed as the container argument. The code to access it would be similar to this one:
    GridViewCell cell = container as GridViewCell;
    var currentColumn = cell.Column as GridViewDataColumn;

    My suggestion is to work with the bound data item and CellStyleSelection and not with the visual elements directly. As RadGridView is a virtualized control, its elements are reused as they are brought in and out the view area. You can also check our online documentation explaining how the UI virtualization works. 

    Regards,
    Dimitrina
    Telerik
    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 Feedback Portal and vote to affect the priority of the items
  6. Tom
    Tom avatar
    4 posts
    Member since:
    May 2016

    Posted 09 Aug in reply to Dimitrina Link to this post

    I found an easier approach using a single ValueConverter (not aware of the DataContext)

    I think this is usually better than the heavy StyleSelector approach, you can also reuse the ValueConverter for different columns more easily.

     

    Set the GridColumns CellStyle to

    <Style x:Key="IsSentCellStyle" TargetType="telerik:GridViewCell" BasedOn="{StaticResource GridViewCellStyle}">
            <Style.Setters>
                <Setter Property="Background"
                        Value="{Binding IsSent, Converter={controls:FalseToRedBrushConverter}}"/>
            </Style.Setters>
        </Style>
     

Back to Top
UI for WPF is Visual Studio 2017 Ready