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

WPF GridView Change Cell Color Based on Value

4 Answers 1974 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Sagar
Top achievements
Rank 1
Sagar asked on 11 Dec 2013, 10:48 AM
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.

4 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 11 Dec 2013, 01:22 PM
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 >>
0
Renier Pretorius
Top achievements
Rank 2
Iron
Iron
Iron
answered on 02 Jun 2015, 03:23 AM

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

0
Dimitrina
Telerik team
answered on 02 Jun 2015, 10:57 AM
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
0
Tom
Top achievements
Rank 1
answered on 09 Aug 2016, 05:19 PM

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>
 

Tags
GridView
Asked by
Sagar
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Renier Pretorius
Top achievements
Rank 2
Iron
Iron
Iron
Tom
Top achievements
Rank 1
Share this question
or