CountFunction with Zero Rows

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

    Posted 02 Dec 2020 Link to this post

    Hi,  How do I get the count function to show '0' when there are no rows on the Grid?  

    The aggregate functions all seem to disappear when there are no rows.

    Thanks,

    Richard

     

    <telerik:GridViewColumn.AggregateFunctions>
        <telerik:CountFunction ResultFormatString=" {0}" />
    </telerik:GridViewColumn.AggregateFunctions>
  2. Answer
    Vladimir Stoyanov
    Admin
    Vladimir Stoyanov avatar
    764 posts

    Posted 03 Dec 2020 Link to this post

    Hello Richard,

    Thank you for the provided code snippet. 

    The aggregate results in the GridViewFooterCell are displayed by an AggregateResultsList element. When there aren't any items in the ItemsSource of the RadGridView the aggregate results are empty. 

    One approach that comes to mind for achieving the desired behavior is to update the ControlTemplate of the GridViewFooterCell. Here is what I have in mind:

    <ControlTemplate x:Key="GridViewFooterCellTemplate" TargetType="telerik:GridViewFooterCell">
                    <Border x:Name="PART_FooterCellBorder"
                    Background="{TemplateBinding Background}"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    BorderThickness="{TemplateBinding BorderThickness}">
                        <Grid>
                            <TextBlock x:Name="emptyContent"
                                    IsHitTestVisible="False"
                                    Margin="{TemplateBinding Padding}"
                                    Text="0"
                                    HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                    VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                    Visibility="Collapsed"/>
                            <ContentPresenter
                        Margin="{TemplateBinding Padding}"
                        Content="{TemplateBinding Content}"
                        ContentTemplate="{TemplateBinding ContentTemplate}"
                        VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                        HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"/>
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <DataTrigger Binding="{Binding Count}" Value="0">
                            <Setter TargetName="emptyContent" Property="Visibility" Value="Visible" />
                        </DataTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
                <Style TargetType="telerik:GridViewFooterCell">
                    <Setter Property="Template" Value="{StaticResource GridViewFooterCellTemplate}"/>
                </Style>

    With this approach an element is conditionally shown only when the aggregate results are empty. Note, that the Count binding in the DataTrigger refers to the Count property of the DataContext inside the GridViewFooterCell and this approach will work for other aggregate functions as well (not only the CountFunction). Also, note that if you are using the NoXaml dlls, you have to base the style on the default one for the theme. 

    Do give this a try and let me know, if it helps. 

    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 03 Dec 2020 in reply to Vladimir Stoyanov Link to this post

    Thank you so much Vladimir, this works perfectly. 

    Richard

Back to Top