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

How to set background color for all cells in a single column

2 Answers 758 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Justin
Top achievements
Rank 1
Justin asked on 03 Jan 2013, 08:12 PM
I have a radgrid set up that displays two columns which are NOT auto generated. Using some of your other examples, I have found a way to color an entire row if the value of the text in a particular column is set to a certain status, using a custom converter class.

I am at a loss however as to how to apply this only to a single column or the cells in that column. In other words how do I change the background color for cells in a single column rather than the entire row or every cell for that matter? This seems like it should be relatively simple, but I have not come across any supporting documentation with a straightforward example.

Here is the XAML I have currently for the grid that sets the entire row color:

My grid.resources definition where I reference my custom class:

<Grid.Resources>
                <local:StatusColorConverter x:Key="ColorConverter"></local:StatusColorConverter>
</Grid.Resources>


XAML For the RadGrid Itself:

<telerik:RadGridView Name="radGrid1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ShowGroupPanel="False" Margin="5,10,0,100" DataLoaded="radGrid1_DataLoaded" SelectionChanged="radGrid1_SelectionChanged" AutoGenerateColumns="False" IsFilteringAllowed="False" ItemsSource="{Binding}" EnableColumnVirtualization="False"  CanUserResizeColumns="False">
                <telerik:RadGridView.Columns>
                    <telerik:GridViewDataColumn Header="Name" DataMemberBinding="{Binding SerialNumber}" IsReadOnly="True" UniqueName="Name" ShowFilterButton="False" AllowDrop="False" FooterTextAlignment="Left" Width="Auto" />
                    <telerik:GridViewDataColumn Header="Status" DataMemberBinding="{Binding StatusDescription}" IsReadOnly="True" UniqueName="Status" ShowFilterButton="False" Width="Auto" />
                </telerik:RadGridView.Columns>               
                <telerik:RadGridView.RowStyle>
                    <Style TargetType="telerik:GridViewRow">
                        <Setter Property="Background" Value="{Binding Status,Converter={StaticResource ColorConverter}}"></Setter>
                    </Style>
                </telerik:RadGridView.RowStyle>
            </telerik:RadGridView>


Custom Color Converter Class:

public class StatusColorConverter: IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Status status = (Status)value;
            SolidColorBrush color = null;
             
            switch (status)
            {
                case Status.Status_A:
                case Status.Status_B:
                 
                color =  new SolidColorBrush(Colors.Red);
 
                    break;
 
                case Status.Status_C:
                    color = new SolidColorBrush(Colors.Green);
                    break;
 
                default:
                    // No color by default.
                    color = new SolidColorBrush();
                    break;
            }
            return color;
        }

Again this works well for coloring the entire row, but I require only the cells in a single column to change color.

Any guidance would be appreciated.

Thank you in advance.

2 Answers, 1 is accepted

Sort by
0
Accepted
Yoan
Telerik team
answered on 04 Jan 2013, 01:07 PM
Hello Justin,

In order to achieve your goal, you can define a style targeting GridViewCell:

<Window.Resources>     
 <Style x:Key="MyCellStyle" TargetType="telerik:GridViewCell">
    <Setter Property="Background" Value="Red"/>
 </Style>
</Window.Resources>

Then you can set the CellStyle property of a particular GridViewDataColumn:

<telerik:GridViewDataColumn DataMemberBinding="{Binding Name}" CellStyle="{StaticResource MyCellStyle}"/>


Please let me know if you need further assistance with this!

Greetings,
Yoan
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Justin
Top achievements
Rank 1
answered on 04 Jan 2013, 04:58 PM
That is perfect thank you very much. And for the record I was able to bind the value property to my custom converter class to handle to logic for coloring.

i.e:
<Window.Resources>
        <local:StatusColorConverter x:Key="ColorConverter"></local:StatusColorConverter>
        <Style x:Key="MyCellStyle" TargetType="telerik:GridViewCell">
            <Setter Property="Background" Value="{Binding Status,Converter={StaticResource ColorConverter}}"></Setter>
        </Style>
</Window.Resources>

Thanks again. You're awesome Yoan.
Tags
GridView
Asked by
Justin
Top achievements
Rank 1
Answers by
Yoan
Telerik team
Justin
Top achievements
Rank 1
Share this question
or