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
>