Setting cell background based on a value

9 posts, 1 answers
  1. Michele
    Michele avatar
    426 posts
    Member since:
    Jun 2009

    Posted 03 Jul 2009 Link to this post

    Hello,
    It's possible to set the background color of a cell (cell, not column or row) based on a particular value??
    Yesterday I spent 2 hours figuring out but I had no luck... I tried on the RowLoaded event... maybe I should do it by javascript??

    Thanks in advance

    Paolo
  2. Answer
    Rossen Hristov
    Admin
    Rossen Hristov avatar
    2477 posts

    Posted 03 Jul 2009 Link to this post

    Hello Paolo,

    The only way I can think of that will work in Silverlight is something like this:

            static void OnRowLoaded(object sender, Telerik.Windows.Controls.GridView.RowLoadedEventArgs e) 
            { 
                Message message = e.DataElement as Message; 
                if (message != null
                { 
                    Binding binding = new Binding("Size"); 
                    binding.Source = message; 
                    binding.Mode = BindingMode.OneWay; 
                    binding.Converter = new SizeToBackgroundConverter(); 
                    e.Row.Cells[2].SetBinding(Control.BackgroundProperty, binding); 
                } 
            } 
     

    I have attached the sample project that this code snippet is from. Inside it you can find the business object class Message that implements INotifyPropertyChanged and the converter used to decide what color to return based on the value. I hope this helps.

    Sincerely yours,
    Ross
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. Marc Roussel
    Marc Roussel avatar
    15 posts
    Member since:
    Aug 2009

    Posted 10 Dec 2009 Link to this post

    This code here works perfectly for me :

    private void rgvTimes_RowLoaded(object sender, Telerik.Windows.Controls.GridView.RowLoadedEventArgs e)  
    {  
        if (e.Row is Telerik.Windows.Controls.GridView.GridViewRow)  
            if (e.DataElement != null)  
            {  
                ProxyCode.Polyrol_Time pt = e.DataElement as ProxyCode.Polyrol_Time;  
     
                //-------------------------------------------------------------------------------------------------------------  
                // If the EndTime of the Time grid is null we set the background to green and if it's not null we set it to red  
                //-------------------------------------------------------------------------------------------------------------  
                e.Row.Cells[5].Background = new SolidColorBrush((pt.EndTime == null ? Colors.Green : Colors.Red));  
            }  
    }  
     

    Hope this helps...
  4. Mark
    Mark avatar
    24 posts
    Member since:
    Jan 2011

    Posted 22 Mar 2011 Link to this post

    Worked like a charm Marc!!! Thanks much.
  5. Giuseppe
    Giuseppe avatar
    17 posts
    Member since:
    Jun 2010

    Posted 25 Mar 2011 Link to this post

    Hi,
     
    I am have done an upgrade to 2011 Q1 from the 2010 Q3 issue ,on previous issue i was setting the backgroud color of a cell and it was working fine , now on the new 2011 issue the colouring of the background (cell) has become inactive. (in this case i cannot use a converter in xaml as i need more than one parameter).

    Here is a snap of my code.

     

    private void syncGrid_CellLoaded ( object sender, Telerik.Windows.Controls.GridView.CellEventArgs e )

     {

     Data.

     

    ThresholdDetail threshold;

     

     Data.Sync row = (Data.Sync)e.Cell.ParentRow.Item;

     

     

        if ( row != null )

     

        {

            switch ( e.Cell.Column.UniqueName )

     

             {
            case "MFP" :

     

     

                threshold = dataContext.Thresholds

                                                .Where ( R => R.hiId ==

     

    "MFP")

     

     

                                                .Where ( R => R.acqTypeName == row.ACQTYPE )

                                                .Where ( R => R.AcqId == row.ACQID )

                                                .FirstOrDefault ();

     

                if ( threshold != null && row.MFP > threshold.daSteadyThrValue )

     

     

                {

                     e.Cell.Background = (

     

    SolidColorBrush)Application.Current.Resources["ThresholdExceedenceColor"];

     

     

                }

                else

     

                {

                     e.Cell.Background =

     

    new SolidColorBrush ( Colors.Transparent );

     

     

                }

     

     

     

        break ;
    ........
    Xaml
    ......
     
    <controls:GridView Name="syncGrid" 

     

     

                        Grid.Row="0" 

     

                        ItemsSource="{Binding Path=Sync}" 

     

                        SelectedItem="{Binding Path=SelectedSync, Mode=TwoWay}"

     

     

                        AutoGenerateColumns="False"

     

                        IsReadOnly="True" 

     

     

     

                        CellLoaded="syncGrid_CellLoaded">

     

     

     

     

     

     

    .......
    The event is fired when the grid is triggerd/loaded but this has no effect on the grid's cell , (2010 Q3 was fine)

    Thank you for your help
    Kind Regards
    Giuseppe

     

     

  6. Yordanka
    Admin
    Yordanka avatar
    634 posts

    Posted 28 Mar 2011 Link to this post

    Hi,

    Thank you for reporting the issue.
    We will log it as a bug and we will try to address it in some of the next internal builds.

    Please excuse us for any inconvenience caused. 

    Kind regards,
    Yordanka
    the Telerik team
  7. Giuseppe
    Giuseppe avatar
    17 posts
    Member since:
    Jun 2010

    Posted 28 Mar 2011 Link to this post

    Hi,

    I have also tried to use RowLoaded event.
    The problem is that only non visible cells are treated only when user moves scrollbars.
    I mean when the grid is displayed  the visibile columns are treated , if there are other columns on the right
    but in that moment not visible, outside the browser page, all the cell conditons/code is lost.

    Awaiting new version.
    Kind Regards, 

    Giuseppe

     
  8. Giuseppe
    Giuseppe avatar
    17 posts
    Member since:
    Jun 2010

    Posted 30 Mar 2011 Link to this post

    Hi Yordanka,
     
    I have tried some other ways to understand better the behaviour posted.
    As far as i can see if CellLoaded or RowLoaded is fired inside the Xaml code where i try
    to chage the background of the cell, this does not work on those cells that are rendered
    on the first page,. Meanwhile if you use the scrollbar then you will have a result on those cell's
    that are re rendered on the page
     
    Otherwise it seems that if you fire the event after  that the Grid has been  rendered
    (doing this from the code-behind) all the cell's will be colored.

    In fact if you use the example source code posted from telerik (in this page)
    you can notice this effect.

    Hope that it is more clear and awiating for the next isssue where i hope it will be resolved.

    Kind Regards,
    Giuseppe 
  9. Yordanka
    Admin
    Yordanka avatar
    634 posts

    Posted 01 Apr 2011 Link to this post

    Hello Paolo,

    Thank you for the details!

    In the meantime, you can use CellStyleSelector to achieve your goals. Here is an online example demonstrating this approach.
     

    Kind regards,
    Yordanka
    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
Back to Top