StyleRule with DataTable as ItemSource

5 posts, 1 answers
  1. Richard
    Richard avatar
    122 posts
    Member since:
    Feb 2009

    Posted 08 Nov 2020 Link to this post

    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>
  2. Vladimir Stoyanov
    Admin
    Vladimir Stoyanov avatar
    764 posts

    Posted 09 Nov 2020 Link to this post

    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/.

  3. Richard
    Richard avatar
    122 posts
    Member since:
    Feb 2009

    Posted 09 Nov 2020 Link to this post

    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

     

  4. Answer
    Vladimir Stoyanov
    Admin
    Vladimir Stoyanov avatar
    764 posts

    Posted 12 Nov 2020 Link to this post

    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/.

  5. Richard
    Richard avatar
    122 posts
    Member since:
    Feb 2009

    Posted 12 Nov 2020 in reply to Vladimir Stoyanov Link to this post

    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 

Back to Top