How to set CellStyle with Auto Generated Columns?

5 posts, 0 answers
  1. Dean K.
    Dean K. avatar
    55 posts
    Member since:
    Aug 2011

    Posted 11 Mar 2014 Link to this post

    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 ?
                }
            }
  2. Yoan
    Admin
    Yoan avatar
    1070 posts

    Posted 14 Mar 2014 Link to this post

    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.

     
  3. UI for WPF is Visual Studio 2017 Ready
  4. Dean K.
    Dean K. avatar
    55 posts
    Member since:
    Aug 2011

    Posted 14 Mar 2014 in reply to Yoan Link to this post

    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...
  5. Yoan
    Admin
    Yoan avatar
    1070 posts

    Posted 17 Mar 2014 Link to this post

    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.

     
  6. Dean K.
    Dean K. avatar
    55 posts
    Member since:
    Aug 2011

    Posted 20 Mar 2014 in reply to Yoan Link to this post

    Yes, that's obviously a better solution, thanks...
Back to Top
UI for WPF is Visual Studio 2017 Ready