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

How to Customize a RowStyle

4 Answers 927 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Vitor
Top achievements
Rank 1
Vitor asked on 30 Mar 2016, 01:40 PM

Hello Telerik,

I am trying to customize a RowStyle, depending on what comes from the database.

My first attempt is very simple, but I am getting this error when running

An unhandled exception of type 'System.InvalidOperationException' occurred in PresentationFramework.dll
Additional information: 'DataGridRow' TargetType does not match type of element 'GridViewRow'.

 

the error makes sense, since telerik GridView is something different than a DataGrid

but I am trying to implement the same approach.

I think it will be more clear when you see my code below:

<telerik:RadGridView Name="RadGridView"  ItemsSource="{Binding View}" Style="{StaticResource VitorStyle}"   AutoGenerateColumns="False">

<Style x:Key="VitorStyle" TargetType="telerik:RadGridView" >
         
        <Setter Property="RowStyle">
            <Setter.Value>
                <Style TargetType="DataGridRow">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Name}" Value="Anastacia Santana">
                            <Setter Property="Background" Value="Red"></Setter>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding Name}" Value="Ana Taylor">
                            <Setter Property="Background" Value="DarkOrchid"></Setter>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Setter.Value>
        </Setter>
    </Style>

As a kickoff test, I would like all Anastacia Santana rows to be RED and all Ana Taylor to be DarkOrchid.

My following tests should envolve a bit wether it is true or false, or a sum result wether it is positive or negative.

 

Can you please show me the best way to achieve this?

as a side note, I would like to keep my XAML as clean as possible. Thus the use of the resource file

 

Thank you very much!

 

4 Answers, 1 is accepted

Sort by
0
Accepted
Stefan Nenchev
Telerik team
answered on 30 Mar 2016, 02:03 PM
Hello Vitor,

The Style that you have defined should target ":GridViewRow". Eventually, you can directly set the Background Property. Please, try the following Style:

<Style x:Key="VitorStyle" TargetType="telerik:GridViewRow" >
            <Style.Triggers>
               <DataTrigger Binding="{Binding Name}" Value="Anastacia Santana">
                    <Setter Property="Background" Value="Red"></Setter>
                </DataTrigger>
               <DataTrigger Binding="{Binding Name}" Value="Ana Taylor">
                    <Setter Property="Background" Value="DarkOrchid"></Setter>
                </DataTrigger>
            </Style.Triggers>
</Style>
And setting the "RowStyle" property of RadGridView:

<telerik:RadGridView Name="RadGridView"  ItemsSource="{Binding View}"  RowStyle="{StaticResource VitorStyle}"   AutoGenerateColumns="False">

Another approach is to use a RowStyleSelector. Please, review the following article from our documentation page - RadGridView - RowStyleSelectors

Regards,
Stefan Nenchev
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
0
Vitor
Top achievements
Rank 1
answered on 01 Apr 2016, 07:23 AM

If i use the RowStyle property of the RadGridView, i get this unpleasing error

 

An unhandled exception of type 'System.InvalidOperationException' occurred in PresentationFramework.dll
Additional information: 'RadGridView' TargetType does not match type of element 'GridViewRow'.

 

I will give a try to the method of the link you quoted and let you know in a moment

0
Vitor
Top achievements
Rank 1
answered on 01 Apr 2016, 08:14 AM

The RowStyleSelector WORKS.

 

thank you!

0
Stefan Nenchev
Telerik team
answered on 01 Apr 2016, 11:45 AM
Hello Vitor,

I am glad to hear that the RowStyleSelector is convenient for you. As for the RowStyle of the RadGridView, please make sure that the Style you have created targets ":GridViewRow" and not ":RadGridView".

Regards,
Stefan Nenchev
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Tags
GridView
Asked by
Vitor
Top achievements
Rank 1
Answers by
Stefan Nenchev
Telerik team
Vitor
Top achievements
Rank 1
Share this question
or