Conditional formatting on cell

16 posts, 1 answers
  1. LE DREAU Steeve
    LE DREAU Steeve avatar
    74 posts
    Member since:
    Aug 2012

    Posted 03 May 2010 Link to this post

    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 !
  2. Answer
    Maya
    Admin
    Maya avatar
    4062 posts

    Posted 03 May 2010 Link to this post

    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.
  3. DevCraft banner
  4. LE DREAU Steeve
    LE DREAU Steeve avatar
    74 posts
    Member since:
    Aug 2012

    Posted 04 May 2010 Link to this post

    Hi,

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

    Regards
    Steeve
  5. Michael
    Michael avatar
    29 posts
    Member since:
    Sep 2008

    Posted 04 Jun 2013 Link to this post

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

    Posted 05 Jun 2013 Link to this post

    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.

  7. Michael
    Michael avatar
    29 posts
    Member since:
    Sep 2008

    Posted 05 Jun 2013 Link to this post

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

    Posted 06 Jun 2013 Link to this post

    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.

  9. Michael
    Michael avatar
    29 posts
    Member since:
    Sep 2008

    Posted 06 Jun 2013 Link to this post

    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}">
  10. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 07 Jun 2013 Link to this post

    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.

  11. Michael
    Michael avatar
    29 posts
    Member since:
    Sep 2008

    Posted 07 Jun 2013 Link to this post

    I have the xaml only approach working now. Thanks for your help.
  12. Eliot
    Eliot avatar
    9 posts
    Member since:
    Dec 2014

    Posted 23 Jan 2015 Link to this post

    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

  13. Stefan X1
    Admin
    Stefan X1 avatar
    517 posts

    Posted 27 Jan 2015 Link to this post

    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.

     
  14. Eliot
    Eliot avatar
    9 posts
    Member since:
    Dec 2014

    Posted 27 Jan 2015 in reply to Stefan X1 Link to this post

    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

  15. Stefan X1
    Admin
    Stefan X1 avatar
    517 posts

    Posted 30 Jan 2015 Link to this post

    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.

     
  16. Eliot
    Eliot avatar
    9 posts
    Member since:
    Dec 2014

    Posted 30 Jan 2015 in reply to Stefan X1 Link to this post

    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
  17. Stefan X1
    Admin
    Stefan X1 avatar
    517 posts

    Posted 30 Jan 2015 Link to this post

    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.

     
Back to Top
DevCraft banner