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

Setting cell background based on a value

8 Answers 165 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Michele
Top achievements
Rank 2
Michele asked on 03 Jul 2009, 07:18 AM
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

8 Answers, 1 is accepted

Sort by
0
Accepted
Rossen Hristov
Telerik team
answered on 03 Jul 2009, 08:55 AM
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.
0
Marc Roussel
Top achievements
Rank 1
answered on 10 Dec 2009, 05:16 PM
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...
0
Mark
Top achievements
Rank 1
answered on 22 Mar 2011, 11:59 PM
Worked like a charm Marc!!! Thanks much.
0
Giuseppe
Top achievements
Rank 1
answered on 25 Mar 2011, 01:09 PM
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

 

 

0
Yordanka
Telerik team
answered on 28 Mar 2011, 12:47 PM
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
0
Giuseppe
Top achievements
Rank 1
answered on 28 Mar 2011, 02:48 PM
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

 
0
Giuseppe
Top achievements
Rank 1
answered on 30 Mar 2011, 03:30 PM
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 
0
Yordanka
Telerik team
answered on 01 Apr 2011, 03:45 PM
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
Tags
GridView
Asked by
Michele
Top achievements
Rank 2
Answers by
Rossen Hristov
Telerik team
Marc Roussel
Top achievements
Rank 1
Mark
Top achievements
Rank 1
Giuseppe
Top achievements
Rank 1
Yordanka
Telerik team
Share this question
or