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

Conditional formatting on cell

15 Answers 288 Views
GridView
This is a migrated thread and some comments may be shown as answers.
LE DREAU Steeve
Top achievements
Rank 1
LE DREAU Steeve asked on 03 May 2010, 08:56 AM
Hi Telerik Team,

It's been a long time I haven't posted a question here :)
I would like to format the background of a cell using it's value.
I've searched about it, but found nothing. I read two weeks ago or last week, that Q1 2010 SP1 would implement such a thing. 

Is there any documentation about that please ?
Thanks,

Regards
Steeve

PS : Thanks for the great job you've made on Reporting and Silverlight controls upgrades !

15 Answers, 1 is accepted

Sort by
0
Accepted
Maya
Telerik team
answered on 03 May 2010, 12:38 PM
Hello LE DREAU Steeve,

With Q1 2010 you are able to apply conditional styles and templates by using DataTemplateSelectors and StyleSelectors for your RadGridView.
I am sending you a sample project that achieves the result you want - to change the background of a cell according to its value. However, you may find additional information on using both DataTemplateSelectors and StyleSelectors in this blog post.
 

Regards,
Maya
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
LE DREAU Steeve
Top achievements
Rank 1
answered on 04 May 2010, 08:07 AM
Hi,

Thanks for your answer ! I knew there were a tutorial somewhere !

Regards
Steeve
0
Michael
Top achievements
Rank 1
answered on 05 Jun 2013, 12:01 AM
I need to change the background color of a cell based on its value at run time. I have tried all three of the examples at the blog post on this subject but have not been able to translate the C# to VB. Can someone give me a solution in VB? I am using version 2012.1.215.1050, .NET 4.0 and Silverlight 5.0. Thanks
0
Maya
Telerik team
answered on 05 Jun 2013, 06:31 AM
Hi Michael,

If you want to change the background, you can work with StyleSelectors. Check out this article and this demo (here the rules for selecting a style are all created in xaml) for more information. 

Regards,
Maya
Telerik

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Michael
Top achievements
Rank 1
answered on 05 Jun 2013, 03:52 PM
In the sample xaml code, there is a prefix named my. I added the StadiumCapacityStyle class to the same vb file in the solution that the rest of the xaml uses.
 
Can you show me how to define the prefix my in the xaml file? I don't have a namespace defined in my vb code since the example didn't either. Is a namespace required? I tried some namespaces but I still cannot get the xaml to recognize the my prefix. I read the article on namespaces but no luck. Thanks
0
Maya
Telerik team
answered on 06 Jun 2013, 06:30 AM
Hello Michael,

This prefix is defined in xaml to point to the namespace of the current project:
xmlns:my="clr-namespace:ConditionalFormatting" 
If you are using it for accessing your StyleSelector, it should point to the place where it is defined (its namespace). 

Regards,
Maya
Telerik

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Michael
Top achievements
Rank 1
answered on 06 Jun 2013, 11:11 PM
I gave up trying to get my namespace to work. I am now trying the xaml only approach from the demo at 
http://demos.telerik.com/silverlight/#GridView/Selectors/StyleSelectors/DeclarativeDefinition

However, I can't get the BasedOn to work. The resource 'GridViewCellStyle' could not be resolved.
What am I missing for that resource?
I am using version: 2012.1.215.1050

Thanks

xmlns:telerikGridView="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView"

BasedOn="{StaticResource GridViewCellStyle}">
0
Maya
Telerik team
answered on 07 Jun 2013, 07:21 AM
Hi Michael,

I would recommend you to run through this article for a reference on how to define custom xaml namespace and resolve the first issue you were having.
As for the style, you could take a look at our documentation for more information. If you are working with normal (reather than with implicit themes and noXaml binaries), you do not need to base your style on the default one.

Regards,
Maya
Telerik

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Michael
Top achievements
Rank 1
answered on 07 Jun 2013, 02:59 PM
I have the xaml only approach working now. Thanks for your help.
0
Eliot
Top achievements
Rank 1
answered on 23 Jan 2015, 10:04 AM
How would you apply Cell Style to dynamically created columns?

As I am pivoting my data I am adding a column for each day between a start and end date at runtime (in the dataloaded handler) as follows:

            foreach (DateTime day in EachDay(_startDate, (DateTime)_endDate))
            {
                columns.Add(new GridViewExpressionColumn() { Header = day.ToString("dd MMM yyyy"), UniqueName = day.ToString("dd MMM yyyy"), DataFormatString = "{0:##,###}", IsReadOnly = true });

                Expression<Func<FUNDING_TRADES, decimal?>> consideration = row => writeConsideration(row, day);
                GridViewExpressionColumn considerationExpression = this.radGridView.Columns[day.ToString("dd MMM yyyy")] as GridViewExpressionColumn;
                considerationExpression.Expression = consideration;
            }  
  

Then I want to loop through the columns and foreach date column format weekends

DateTime columnDate;


 foreach (Telerik.Windows.Controls.GridViewColumn column in radGridView.Columns)
            {              
                if (column != null && DateTime.TryParse(column.Header.ToString(), out columnDate))
        {
if(columnDate.DayOfWeek == DayOfWeek.Saturday || columnDate.DayOfWeek == DayOfWeek.Sunday)
                   {
//applyweekend format
           }
        else
{
//apply week format
}
........



How can I do this programatically?
I've been looking at using the ConditionalStyleSelector and stylerules but haven't been able to get it to work

thanks

0
Stefan
Telerik team
answered on 27 Jan 2015, 07:47 AM
Hi Elliot,

To achieve your goal you should be using CellStyleSelector. Please check the above documentation article for further reference. However, I set up a sample project for your convenience.

Let me know if you need any further assistance.

Kind Regards,
Stefan
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Eliot
Top achievements
Rank 1
answered on 27 Jan 2015, 12:00 PM
Hi Stefan,

thanks for the project.

Unfortunately that doesn't work for me.
I have seen that example before.

I am adding columns at runtime as I don't know the number of columns I will need (is one per day between a start and end date determined at runtime).

columns.Add(new GridViewExpressionColumn() { Header = day.ToString("dd MMM yyyy"), UniqueName = day.ToString("dd MMM yyyy"), DataFormatString = "{0:##,###}", IsReadOnly = true });

So I can't define the CellStyleSelector in XAML at design time.
I need a way to be able to add a CellStyleSelector or some other method at runtime once I have programatically created the columm and added it to the columns collection.

Hope that makes sense,

Eliot

0
Stefan
Telerik team
answered on 30 Jan 2015, 09:39 AM
Hi Eliot,

If I am getting you right you want to have different Style for the weekdays and the weekend ones. If that is your goal, then CellStyleSelector is exactly what you need. The number of columns is irrelevant to this case.You will just need to define your CellStyleSelector in XAML and set it programmatically to the dynamically created columns. You could  check programmatically if a particular day is from the workweek or from the weekend and simply return the Style according to that day.

I have modified the sample project in a way that it reproduces the above scenario.

I hope that this helps.

Kind Regards,
Stefan
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Eliot
Top achievements
Rank 1
answered on 30 Jan 2015, 09:43 AM
Hi Stefan,

I ended up using CellStyle.
In my App.xaml I declared styles as follows:

<Application.Resources>
        <Style TargetType="{x:Type telerik:GridViewCell}" x:Key="WeekEndStyle">
            <Setter Property="Background" Value="LightGray" />
        </Style>

        <Style TargetType="{x:Type telerik:GridViewCell}" x:Key="WeekDayStyle">
            <Setter Property="Background" Value="White" />
        </Style>

Then in radGridView_DataLoaded I assign the style resource to CellStyle

                if (day.DayOfWeek == DayOfWeek.Saturday || day.DayOfWeek == DayOfWeek.Sunday)
                {
                    dateColumn.CellStyle = Application.Current.Resources["WeekEndStyle"] as Style;
                }
                else
                {
                    dateColumn.CellStyle = Application.Current.Resources["WeekDayStyle"] as Style;
                }
          


Regards,

Eliot
0
Stefan
Telerik team
answered on 30 Jan 2015, 04:33 PM
Hi Eliot,

I am glad that you have found solution suitable for your needs.

We will be glad to provide further assistance if it is needed.

Regards,
Stefan
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
GridView
Asked by
LE DREAU Steeve
Top achievements
Rank 1
Answers by
Maya
Telerik team
LE DREAU Steeve
Top achievements
Rank 1
Michael
Top achievements
Rank 1
Eliot
Top achievements
Rank 1
Stefan
Telerik team
Share this question
or