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

Apply CellStyleSelector in code behind

2 Answers 395 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Stas
Top achievements
Rank 1
Stas asked on 21 Mar 2016, 06:17 PM

I create class for style selector as follows:

public class NegativeNumberStyle : StyleSelector     {         

    public override Style SelectStyle(object item, DependencyObject container)         {             

         GridViewCell cell = container as GridViewCell;
         var value = item.GetType().GetProperty(cell.Column.UniqueName).GetValue(item, null);
         if (value.GetType() == typeof(int) && ((int)value) < 0) 
return NegativeStyle;
         if (value.GetType() == typeof(decimal) && ((decimal)value) < 0)  return NegativeStyle;
         
return null; 
    
}           

     public Style NegativeStyle { get; set; }
}

 

Then I add the resources to the grid on my page as follows:

        <Grid.Resources>
 <my:NegativeNumberStyle x:Key="negativeNumberStyle">
<my:NegativeNumberStyle.NegativeStyle>
<Style TargetType="telerik:GridViewCell" BasedOn="StaticResource GridViewCellStyle}">
<Setter Property="Background" Value="Red"/>
</Style>
</my:NegativeNumberStyle.NegativeStyle>
</my:NegativeNumberStyle>
</Grid.Recources>

Since I load columns dynamically I set Columns CellStyle property in code behind

GridViewDataColumn gridColumn = new GridViewDataColumn();

gridColumn.CellStyleSelector = new NegativeNumberStyle();

 

But negative numbers aren't use the proper style (not showing in red color). I debug the code and noticed that NegativeProperty always returns null.

 

What am I doing wrong?

 

Thanks

2 Answers, 1 is accepted

Sort by
0
Accepted
Stefan Nenchev
Telerik team
answered on 22 Mar 2016, 09:43 AM
Hi Stas,

The approach of applying a CellStyleSelector can be reviewed at the following article from our documentation page - Conditionally Apply a Style. As you can see, you need to work with the actual item and not directly with the GridViewCell. Generally, we do not recommend working with the visual elements as this might result in inconsistent behavior due to the UI Virtualization support of the RadGridView.

Stas, I have attached a sample project that implements the desired behavior for applying a Style for the negative values of a certain column. Please review it and consider such approach at your end.

Regards,
Stefan Nenchev
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
0
Stas
Top achievements
Rank 1
answered on 22 Mar 2016, 04:15 PM

Hi Stephan,

I was following this article. The only problem is that itc doesn't show how to apply StyleSelector from code behind. Looking in you example I found the problem.

I replace gridColumn.CellStyleSelector = new NegativeNumberStyle(); with gridColumn.CellStyleSelector = Resources["NegativeNumberStyle"] as StyleSelector; and now it works

 

Thanks

Bernhard
Top achievements
Rank 1
commented on 05 Nov 2021, 02:57 PM

Hi Stas

You saved my life - thanks a lot.

And no, I am not worried about virtualization as I have only 18 lines in my grid.

Cheers, Boerns

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