Visibility of grid inside grid

3 posts, 0 answers
  1. Goran
    Goran avatar
    77 posts
    Member since:
    Feb 2011

    Posted 14 Aug 2012 Link to this post


    I am in a scenratio where I have grid inside grid (HierarchyChildTemplate). Not all rows in outer grid have children records, so inside grid is visible for rows that do. I allow user to add/remove/edit children data.

    Lets say that I have Groups and Products

    Group A
    Group B
        Product A
        Product B
        Product C
        Product D

    AS it can be seen, ProductA and B belong to GroupB, and ProductC and D belong to GroupD. GroupA and GroupC don't have products in them.

    1) If I click on some button, which executes a command on ViewModel, and some product is added to GroupA, RadGridView will not automatically display children records. In order for new product ProductE to appear under GroupA, I needed to execute below code each time a new product is added:

    groupGrid.ItemContainerGenerator.ContainerFromItem(groupGrid.SelectedItem) as GridViewRow;
    if (row != null)
        row.IsExpandable = true;
        row.IsExpanded = true;

    Is this the correct way to do this, and why the grid doesn't sense the notifications from ObservableCollection of Products?

    2) If I remove ProductC and ProductD, then GroupD should hidechild grid. However, if at this point I insert a new product inside GroupD, child grid should appear again. I am having difficulties in implementing this requirement. If I bind to Visibility, I could solve the problem with inserting / removing products, since I am ading/removing in ViewModel, I just need to set value for ProductsExist in AddProduct/RemoveProduct commands.

    Visibility="{Binding ProductsExist, Source={StaticResource viewModel}, Converter={StaticResource boolToVisibilityConverter}}"

    However, the problem appears during data loading, Since this is asyncronous process, I would need to set some flags to know when loading is finished, so I can correct the value for ProductsExist.

    I am finding all this a bit dirty. What are your suggestions to solve this problems of appearance with inside grid? And why is this not automatic, if child grid is bound to ObservableCollection<Product>? It can pick up notifications from INCC and react accordingly.

  2. Goran
    Goran avatar
    77 posts
    Member since:
    Feb 2011

    Posted 14 Aug 2012 Link to this post

    in addition to second point, setting the flag will not have proper results, since templates are recreated after the data is fetched, so IsLoading flag will always be false.
  3. Dimitrina
    Dimitrina avatar
    3769 posts

    Posted 20 Aug 2012 Link to this post

    Hi Goran,

    Indeed, the GridViewRows does not listen for such notifications and they cannot automatically update their state. This is done by design.

    When you manually update the IsExpanded/IsExpandable states you should be able to achieve a consistent state of showing the expand/collapse state. This is a correct way.

    I am not sure what exactly do you do with approach two. If approach 1 works for you, please use it.

    Kind regards,
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top