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

[Solved] Column CellStyle

4 Answers 179 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
John Giblin
Top achievements
Rank 1
John Giblin asked on 04 Aug 2011, 04:52 PM
I am having trouble setting the Style of the of a GridViewDataColumn.  Whenever I set the CellStyle, the app crashes.  Below is what I have.

App.XMAL

        <Style x:Key="colRank" TargetType="telerik:GridViewDataColumn">
            <Setter Property="Width" Value="25"></Setter>
            <Setter Property="Background"  Value="#66CC00"></Setter>
        </Style>
        <Style x:Key="colProgram" TargetType="telerik:GridViewDataColumn">
            <Setter Property="Width" Value="150"></Setter>
            <Setter Property="Background"  Value="#E4E4DE"></Setter>
        </Style>
        <Style x:Key="colNet" TargetType="telerik:GridViewDataColumn">
            <Setter Property="Width" Value="45"></Setter>
            <Setter Property="Background"  Value="#A1A19A"></Setter>
        </Style>
        <Style x:Key="colDate" TargetType="telerik:GridViewDataColumn">
            <Setter Property="Background"  Value="#E4E4DE"></Setter>
            <Setter Property="DataFormatString" Value="{}{0:M/dd/yyyy}"></Setter>
        </Style>
        <Style x:Key="colTime" TargetType="telerik:GridViewDataColumn">
            <Setter Property="Background"  Value="#E4E4DE"></Setter>
            <Setter Property="DataFormatString" Value="{}{0:HH:mm}"></Setter>
        </Style>
        <Style x:Key="colGeneric" TargetType="telerik:GridViewDataColumn">
            <Setter Property="Background"  Value="#E4E4DE"></Setter>
        </Style>

Page.XMAL.cs
rgvResults.Columns[rcbDemo.SelectionBoxItem.ToString() + "_RANK"].CellStyle = (Style)Application.Current.Resources["colRank"];
rgvResults.Columns["PROGRAM"].CellStyle = (Style)Application.Current.Resources["colProgram"];
rgvResults.Columns["NET"].CellStyle = (Style)Application.Current.Resources["colNet"];
rgvResults.Columns["S_DATE"].CellStyle = (Style)Application.Current.Resources["colDate"];
rgvResults.Columns["DOW"].CellStyle = (Style)Application.Current.Resources["colRank"];
rgvResults.Columns["S_TIME"].CellStyle = (Style)Application.Current.Resources["colTime"];
rgvResults.Columns[rcbDemo.SelectionBoxItem.ToString() + "_000"].CellStyle = (Style)Application.Current.Resources["colGeneric"];

4 Answers, 1 is accepted

Sort by
0
Vanya Pavlova
Telerik team
answered on 04 Aug 2011, 05:51 PM
Hi John,

 

You have created a style targeted at GridViewDataColumn. Since the columns in RadGridView are not visual objects you cannot create a style targeted at a column. Just change the TargetType to GridViewCell and it should work:


<Style x:Key="colRank" TargetType="telerik:GridViewCell">
           <Setter Property="Width" Value="25"></Setter>
           <Setter Property="Background"  Value="#66CC00"></Setter>
       </Style>


Greetings,
Vanya Pavlova
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

0
John Giblin
Top achievements
Rank 1
answered on 04 Aug 2011, 09:13 PM
thanks for your help. that worked

I am still having a few issues.  First of all I am not able to set the DataFormatString since it is now GridViewCell

I tried it programically
((GridViewDataColumn)rgvResults.Columns["S_TIME"]).DataFormatString = "{}{0:HH:mm}";

Also when I set the width,  the Background color will be that width even when the the width comes out to be wider.

0
Vanya Pavlova
Telerik team
answered on 05 Aug 2011, 08:34 AM
Hi John Giblin,

 

Indeed you are right! Setting data format string cannot be done at a cell level, this property may be set only on GridViewDataColumn level.  Perhaps you are getting some error when defining DataFormatString in code-behind. Generally you need the leading {} only when you define the DataFormatString property in XAML. In code-behind you can only use {0:specified format}:


( ( GridViewDataColumn )this.radGridView.Columns[ "OrderDate" ] ).DataFormatString = "{0:dd, MMM, yyyy}";


Regarding your second question the behavior you described is the expected one. When you set either the Background property of a column or define the background for the cells in this column using a style the color will fill the available space.  

Regards,
Vanya Pavlova
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

0
John Giblin
Top achievements
Rank 1
answered on 05 Aug 2011, 04:59 PM
Thanks!  I thought I tried that. I think my DataFormatstring was incorrect.

Tags
GridView
Asked by
John Giblin
Top achievements
Rank 1
Answers by
Vanya Pavlova
Telerik team
John Giblin
Top achievements
Rank 1
Share this question
or