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

How to set CellStyle with Auto Generated Columns?

4 Answers 168 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Dean
Top achievements
Rank 1
Dean asked on 11 Mar 2014, 05:21 PM
How to set CellStyle with Auto Generated Columns?

CellStyle is usually set within GridViewDataColumn <t:GridViewDataColumn CellStyle="{StaticResource MyCellStyle}"

But what if AutoGenerateColumns is set to true?

I tried in code behind but couldn't find what to cast the object FindResouce returns to:

        private void GridViewDataControl_OnDataLoading(object sender, GridViewDataLoadingEventArgs e)
        {
            var rgv = sender as RadGridView;
            if (rgv == null) return;
            foreach (var col in rgv.Columns)
            {
                col.CellStyle = FindResource("MyCellStyle") as ?
            }
        }

4 Answers, 1 is accepted

Sort by
0
Yoan
Telerik team
answered on 14 Mar 2014, 03:32 PM
Hello Dean ,

You can use RadGridView's DataLoaded event instead of DataLoading:
private void clubsGrid_DataLoaded(object sender, EventArgs e)
        {
            var rgv = sender as RadGridView;
            if (rgv == null) return;
            foreach (var col in rgv.Columns)
            {
                col.CellStyle = FindResource("myCellStyle") as Style;
            }
        }

Generally, if you want to apply the style to all columns, then you can define it as implicit(without x:Key attribute):
<Window.Resources>
        <Style x:Key="myCellStyle" TargetType="telerik:GridViewCell" >
            <Setter Property="Background" Value="Red"/>
        </Style>
        <Style  TargetType="telerik:GridViewCell" >
            <Setter Property="Background" Value="Red"/>
        </Style>      
    </Window.Resources>

If you want to apply it to a specific column, you can use RadGridView's AutoGeneratingColumn event. Please check the following code snippet for a reference:
private void clubsGrid_AutoGeneratingColumn_1(object sender, GridViewAutoGeneratingColumnEventArgs e)
        {
            if (e.Column.UniqueName=="Name")
            {
              e.Column.CellStyle = FindResource("myCellStyle") as Style;
            }
        }

I hope this information helps.

Regards,
Yoan
Telerik
 

DevCraft Q1'14 is here! Watch the online conference to see how this release solves your top-5 .NET challenges. Watch on demand now.

 
0
Dean
Top achievements
Rank 1
answered on 14 Mar 2014, 09:47 PM
Thanks, as Style worked and no I can't use Implicit style because I need to apply this style only to special case Grid Views...
0
Yoan
Telerik team
answered on 17 Mar 2014, 05:09 PM
Hi Dean ,

In this case, you can add an implicit style in RadGridView's Resources:
<telerik:RadGridView x:Name="rgv1">
<telerik:RadGridView.Resources>
        <Style  TargetType="telerik:GridViewCell" >
            <Setter Property="Background" Value="Red"/>
        </Style>
</telerik:RadGridView.Resources>
                      .
                      .
In this way, the style will be applied to all GridViewCells in a specific RadGridView.


Regards,
Yoan
Telerik
 

DevCraft Q1'14 is here! Watch the online conference to see how this release solves your top-5 .NET challenges. Watch on demand now.

 
0
Dean
Top achievements
Rank 1
answered on 20 Mar 2014, 03:57 PM
Yes, that's obviously a better solution, thanks...
Tags
GridView
Asked by
Dean
Top achievements
Rank 1
Answers by
Yoan
Telerik team
Dean
Top achievements
Rank 1
Share this question
or