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

StyleRule with DataTable as ItemSource

4 Answers 201 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Richard
Top achievements
Rank 2
Iron
Veteran
Iron
Richard asked on 08 Nov 2020, 10:18 PM

How do I get StyleRules to work with a  DataTable as ItemSource of the GridView? 

 

I have some sample code that has two grid views with the same StyleRules but one uses DataTable and the other uses a list of object 'item'. The list of objects respects the StyleRules but the grid with the DataTable does not and I'm not sure why?

 

DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Value", typeof(int));
 
var dr = dt.NewRow();
dr["ID"] = 1;
dr["Value"] = 0;
dt.Rows.Add(dr);
 
dr = dt.NewRow();
dr["ID"] = 2;
dr["Value"] = 1;
dt.Rows.Add(dr);
 
dr = dt.NewRow();
dr["ID"] = 3;
dr["Value"] = 2;
dt.Rows.Add(dr);
  
RadGridView1.ItemsSource = dt;
 
 
List<Item> items = new List<Item>(){ new Item(1,0), new Item(2, 1) , new Item(3, 2) };
RadGridView2.ItemsSource = items;

 

<Grid>
<Grid.RowDefinitions>
    <RowDefinition Height="Auto"/>
    <RowDefinition Height="Auto"/>
    <RowDefinition Height="Auto"/>
    <RowDefinition Height="Auto"/>
    <RowDefinition Height="*"/>
</Grid.RowDefinitions>
 
<TextBlock Grid.Row="1" FontSize="16" Margin="0,0,0,5" Text="Data Table Item Source"/>
<telerik:RadGridView Grid.Row="2" x:Name="RadGridView1" AutoGenerateColumns="False" ShowGroupPanel="False" >
    <telerik:RadGridView.RowStyleSelector>
        <telerik:ConditionalStyleSelector>
            <telerik:StyleRule Condition="Value > 1">
                <Style TargetType="telerik:GridViewRow">
                    <Setter Property="Background" Value="Red"/>
                </Style>
            </telerik:StyleRule>
            <telerik:StyleRule Condition="Value = 1">
                <Style TargetType="telerik:GridViewRow">
                    <Setter Property="Background" Value="Green"/>
                </Style>
            </telerik:StyleRule>
        </telerik:ConditionalStyleSelector>
    </telerik:RadGridView.RowStyleSelector>
    <telerik:RadGridView.Columns>
        <telerik:GridViewDataColumn DataMemberBinding="{Binding ID}" />
        <telerik:GridViewDataColumn DataMemberBinding="{Binding Value}" />
    </telerik:RadGridView.Columns>
</telerik:RadGridView>
 
 
<TextBlock Grid.Row="3" FontSize="16" Margin="0,10,0,5" Text="List Object Item Source"/>
<telerik:RadGridView Grid.Row="4" x:Name="RadGridView2" AutoGenerateColumns="False" ShowGroupPanel="False"  >
    <telerik:RadGridView.RowStyleSelector>
        <telerik:ConditionalStyleSelector>
            <telerik:StyleRule Condition="Value > 1">
                <Style TargetType="telerik:GridViewRow">
                    <Setter Property="Background" Value="Red"/>
                </Style>
            </telerik:StyleRule>
            <telerik:StyleRule Condition="Value = 1">
                <Style TargetType="telerik:GridViewRow">
                    <Setter Property="Background" Value="Green"/>
                </Style>
            </telerik:StyleRule>
        </telerik:ConditionalStyleSelector>
    </telerik:RadGridView.RowStyleSelector>
    <telerik:RadGridView.Columns>
        <telerik:GridViewDataColumn DataMemberBinding="{Binding ID}" />
        <telerik:GridViewDataColumn DataMemberBinding="{Binding Value}" />
    </telerik:RadGridView.Columns>
</telerik:RadGridView>
 
</Grid>

4 Answers, 1 is accepted

Sort by
0
Vladimir Stoyanov
Telerik team
answered on 09 Nov 2020, 10:33 AM

Hello Richard,

Thank you for the shared picture and sample code. 

Speaking generally, when using a DataTable it is recommended to set the ItemsSource of the RadGridView to the DefaultView property. 

In that scenario, you will have to create a custom RowStyleSelector and implement the desired logic. Please, find a sample project that demonstrates this approach.

I hope you find this helpful.  

Regards,
Vladimir Stoyanov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Richard
Top achievements
Rank 2
Iron
Veteran
Iron
answered on 09 Nov 2020, 10:49 AM

Thanks Vladimir, 
Does that mean that StyleRules don't work with a DataTable?  I main reason I ask is that I was hopping to allow the Styles to be created dynamically in some sort of Admin UI and then I would apply them at run time using the Telerik ExpressionTypeConverter. 

e.g. 

var expressionReadFromSettings = "Value > 1"
 
var rule = new StyleRule
{
    Style = style,
    Condition = (Expression) expressionTypeConverter.ConvertFrom(expressionReadFromSettings)
};
 
// Add rule to a ConditionalStyleSelector ...etc

 

0
Accepted
Vladimir Stoyanov
Telerik team
answered on 12 Nov 2020, 09:41 AM

Hello Richard,

Thank you for the update. 

Indeed the ExpressionTypeConverter can only generate expressions for simple objects. That said, you can use the RadExpressionEditor, which is more powerful and can generate an expression in the context of the object that is passed to its Item property. 

Please, find the sample project updated to demonstrate its usage.

Regards,
Vladimir Stoyanov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Richard
Top achievements
Rank 2
Iron
Veteran
Iron
answered on 12 Nov 2020, 10:32 AM

Thank you Vladimir! that's exactly what I was looking for. I really appreciate your help and your knowledge on these tools, its excellent.

Richard 

Tags
GridView
Asked by
Richard
Top achievements
Rank 2
Iron
Veteran
Iron
Answers by
Vladimir Stoyanov
Telerik team
Richard
Top achievements
Rank 2
Iron
Veteran
Iron
Share this question
or