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

How to hide empty grids when using a hierarchy

1 Answer 88 Views
GridView
This is a migrated thread and some comments may be shown as answers.
James
Top achievements
Rank 1
James asked on 16 Jan 2012, 02:43 PM

I am using a RadGridView to display some data in a hierarchy. I am using MvvM so I am building by grid in XAML and have very little code behind.

I have a very simple hierarchy where I have a list of objects (Agents) and each agent can have a list of sub agents and a list of clients. These lists are two separate properties of my agent object, i.e. Agent.Agents and Agent.ClientAccounts. An agent can have a list of clients and a list of sub agents so when the user expands an agent row they will see two child grids at the same level in the hierarchy.

I am using the grid_RowLoaded event to only allow expanding when I have either a list of SubAgents or a list of Clients.

My issue is that when one of the lists of either SubAgents or Clients doesn’t have any records I would like to hide the grid (or not even create it in the first place)

I have tried to set the visibility of the SubAgents and Clients  grids by using a converter so the grids are hidden when the lists have a count of 0 but that didn’t work

Any ideas would be much appreciated

Here is the structure of my grid

<telerik:RadGridView 
        x:Name="grid"
        BorderThickness="0"
        Background="Transparent"
        ShowGroupPanel="False" 
        IsReadOnly="True"
        DataContext="{Binding}" 
        ItemsSource="{Binding Path=AgentList}" 
        RowIndicatorVisibility="Collapsed" 
        GridLinesVisibility="None" 
        telerik:Theming.Theme="{DynamicResource TelerikThemeName}" 
        IsSynchronizedWithCurrentItem="True"
        ShowColumnHeaders="True" 
        AutoGenerateColumns="False" 
        acb:CommandBehavior.Event="MouseDoubleClick"  
        acb:CommandBehavior.Command="{Binding OpenAgentDetailsCommand}" 
        acb:CommandBehavior.CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=SelectedItem.CustomerId}" RowLoaded="grid_RowLoaded">
 
        <telerik:RadGridView.ChildTableDefinitions>
            <telerik:GridViewTableDefinition/>
        </telerik:RadGridView.ChildTableDefinitions>
 
        <telerik:RadGridView.Columns>
            <telerik:GridViewDataColumn DataMemberBinding="{Binding AccountId}" Header="Account Id" Width="Auto" />
            <telerik:GridViewDataColumn DataMemberBinding="{Binding CustomerId}" Header="CustomerId" Width="Auto" />
            <telerik:GridViewDataColumn DataMemberBinding="{Binding FirstName}" Header="FirstName" Width="Auto"/>
            <telerik:GridViewDataColumn DataMemberBinding="{Binding LastName}" Header="LastName" Width="Auto"/>
            <telerik:GridViewDataColumn DataMemberBinding="{Binding Volume}" Header="Volume" Width="Auto" DataFormatString="{}{0:N0}"/>
        </telerik:RadGridView.Columns>
        
        <telerik:RadGridView.HierarchyChildTemplate>
            <DataTemplate>
                <StackPanel>
 
                    <!--sub agents-->
                    <telerik:RadGridView ItemsSource="{Binding Path=Agents}" AutoGenerateColumns="False" ShowGroupPanel="False" 
                                         Visibility="{Binding Path=Agents.Count, Converter={StaticResource countToVisibleConverter}}">
                        <telerik:RadGridView.Columns>
                            <telerik:GridViewDataColumn DataMemberBinding="{Binding AccountId}" Header="Account Id" Width="Auto" />
                            <telerik:GridViewDataColumn DataMemberBinding="{Binding CustomerId}" Header="CustomerId" Width="Auto" />
                            <telerik:GridViewDataColumn DataMemberBinding="{Binding FirstName}" Header="FirstName" Width="Auto"/>
                            <telerik:GridViewDataColumn DataMemberBinding="{Binding LastName}" Header="LastName" Width="Auto"/>
                            <telerik:GridViewDataColumn DataMemberBinding="{Binding Volume}" Header="Volume" Width="Auto" DataFormatString="{}{0:N0}"/>
                        </telerik:RadGridView.Columns>
 
                        <telerik:RadGridView.ChildTableDefinitions>
                            <telerik:GridViewTableDefinition />
                        </telerik:RadGridView.ChildTableDefinitions>
 
                        <!--sub agents : clients-->
                        <telerik:RadGridView.HierarchyChildTemplate>
                            <DataTemplate>
                                <telerik:RadGridView ItemsSource="{Binding Path=ClientAccounts}" AutoGenerateColumns="False" ShowGroupPanel="False" 
                                                     Visibility="{Binding Path=ClientAccounts.Count, Converter={StaticResource countToVisibleConverter}}">
                                    <telerik:RadGridView.Columns>
                                        <telerik:GridViewDataColumn DataMemberBinding="{Binding AccountId}" Header="Account Id" Width="Auto" />
                                        <telerik:GridViewDataColumn DataMemberBinding="{Binding FirstName}" Header="FirstName" Width="Auto"/>
                                        <telerik:GridViewDataColumn DataMemberBinding="{Binding LastName}" Header="LastName" Width="Auto"/>
                                        <telerik:GridViewDataColumn DataMemberBinding="{Binding Volume}" Header="Volume" Width="Auto" DataFormatString="{}{0:N0}"/>
                                    </telerik:RadGridView.Columns>
                                </telerik:RadGridView>
                            </DataTemplate>
                        </telerik:RadGridView.HierarchyChildTemplate>
 
                    </telerik:RadGridView>
 
                    <!--clients-->
                    <telerik:RadGridView ItemsSource="{Binding Path=ClientAccounts}" AutoGenerateColumns="False" ShowGroupPanel="False" 
                                         Visibility="{Binding Path=ClientAccounts.Count, Converter={StaticResource countToVisibleConverter}}">
                        <telerik:RadGridView.Columns>
                            <telerik:GridViewDataColumn DataMemberBinding="{Binding AccountId}" Header="Account Id" Width="Auto" />
                            <telerik:GridViewDataColumn DataMemberBinding="{Binding FirstName}" Header="FirstName" Width="Auto"/>
                            <telerik:GridViewDataColumn DataMemberBinding="{Binding LastName}" Header="LastName" Width="Auto"/>
                            <telerik:GridViewDataColumn DataMemberBinding="{Binding Volume}" Header="Volume" Width="Auto" DataFormatString="{}{0:N0}"/>
                        </telerik:RadGridView.Columns>
                    </telerik:RadGridView>
 
                </StackPanel>
                                        
            </DataTemplate>
        </telerik:RadGridView.HierarchyChildTemplate>
 
    </telerik:RadGridView>

1 Answer, 1 is accepted

Sort by
0
James
Top achievements
Rank 1
answered on 16 Jan 2012, 03:03 PM
I seem to have fixed this issue. My object properties were not notifying change and I have also used the telerik boolean converter rather than my existing custom converter, and I now have a boolean  property to indicate if the lists exist (HasAgents + HasClientAccounts)  ...one / both or all of these changes has fixed the issue
Tags
GridView
Asked by
James
Top achievements
Rank 1
Answers by
James
Top achievements
Rank 1
Share this question
or