UWP DataGrid Collapse All Groups

9 posts, 0 answers
  1. Jeff
    Jeff avatar
    8 posts
    Member since:
    Jun 2016

    Posted 22 Aug Link to this post

    I'm loading data into a UWP DataGrid, but I don't see a way to collapse all of the groups after loading the grid.ItemsSource.

    Is there a way to do this?

  2. Jeff
    Jeff avatar
    8 posts
    Member since:
    Jun 2016

    Posted 23 Aug in reply to Jeff Link to this post

    Does Telerik monitor these forums?
  3. Stefan Nenchev
    Admin
    Stefan Nenchev avatar
    503 posts

    Posted 25 Aug Link to this post

    Hi Jeff,

    We are checking the forum threads when we have the chance but they are not an official support channel and are mainly created for the Telerik community so that different topics are discussed by its members. For official support in the future, please raise ticket so we can address your specific issue.

    With the current implementation of the RadDataGrid and its grouping mechanism, collapsing/expanding the groups is not supported. I have logged this functionality as a feature request at our end, you can follow the public item at the following link - DataGrid: Add support for expanding/collapsing groups programmatically.

    The only thing we can suggest, which partially works and does not cover most scenarios is the following:

    private void Button_Click(object sender, RoutedEventArgs e)
            {
                var dataGridGroupHeaders = ElementTreeHelper.EnumVisualDescendants(this.grid, a => a.GetType() == typeof(DataGridGroupHeader));
                var panel = ElementTreeHelper.EnumVisualDescendants(this.grid, a => a.GetType() == typeof(Canvas) && ((Panel)a).Name == "PART_GroupHeadersHost") as Canvas;
                if (dataGridGroupHeaders.Count() > 0)
                {
                    foreach (DataGridGroupHeader groupHeader in dataGridGroupHeaders)
                    {
                      
                        this.grid.CommandService.ExecuteCommand(CommandId.GroupHeaderTap, groupHeader.DataContext);
                    }
                }
            }

     This will collapse/expand the groups, however, it will be applied only to those elements which are present in the visual tree. This would mean that if you have more items and respectively groups, some of them which are not visible will not be affected by the command. You can use it in a very specific scenario where the RadDataGrid contains fewer items.

    Regards,
    Stefan Nenchev
    Progress Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  4. Jeff
    Jeff avatar
    8 posts
    Member since:
    Jun 2016

    Posted 25 Aug in reply to Stefan Nenchev Link to this post

    dataGridGroupHeaders.Count() doesn't exist it says...

    Are these the correct using statements I should have for this?

     

    using Telerik.Core;
    using Telerik.UI.Xaml.Controls.Grid.Commands;
    using Telerik.UI.Xaml.Controls.Grid.Primitives;

  5. Stefan Nenchev
    Admin
    Stefan Nenchev avatar
    503 posts

    Posted 28 Aug Link to this post

    Hello Jeff,

    Can you please make sure that you have added a reference to System.Linq so you can use the Count extension method?

    using System.Linq;

    I have attached a sample for your reference. Please have a look at it and consider whether the approach would work for you. I would like to once again mention that it will work in very particular scenarios only and is definitely not a permanent solution.

    Have a great week.

    Regards,
    Stefan Nenchev
    Progress Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  6. Jeff
    Jeff avatar
    8 posts
    Member since:
    Jun 2016

    Posted 28 Aug Link to this post

    I added that, but count is 0. We are calling this immediately after binding the data, and it's not working. Is there an on after render event we can use and fire this programatically after we bind the data?
  7. Stefan Nenchev
    Admin
    Stefan Nenchev avatar
    503 posts

    Posted 31 Aug Link to this post

    Hello Jeff,

    The approach I have suggested works with the visual elements of the DataGrid. With this in mind, in order for you to iterate these visual elements - they need to be rendered first, so it seems that you are applying the logic before the groups are rendered. Please, make sure to add the logic once they are visualized. You can do this in the Loaded event of the RadDataGrid and slow down the action a bit:

    private async void grid_Loaded(object sender, RoutedEventArgs e)
           {
               await Task.Delay(TimeSpan.FromSeconds(0.1));
                
               var dataGridGroupHeaders = ElementTreeHelper.EnumVisualDescendants(this.grid, a => a.GetType() == typeof(DataGridGroupHeader));
               var panel = ElementTreeHelper.EnumVisualDescendants(this.grid, a => a.GetType() == typeof(Canvas) && ((Panel)a).Name == "PART_GroupHeadersHost") as Canvas;
               if (dataGridGroupHeaders.Count() > 0)
               {
                   foreach (DataGridGroupHeader groupHeader in dataGridGroupHeaders)
                   {
     
                       this.grid.CommandService.ExecuteCommand(CommandId.GroupHeaderTap, groupHeader.DataContext);
                   }
               }
           }

    Regards,
    Stefan Nenchev
    Progress Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  8. Jeff
    Jeff avatar
    8 posts
    Member since:
    Jun 2016

    Posted 31 Aug in reply to Stefan Nenchev Link to this post

    So, it actually ends up hitting this code twice, but it doesn't collapse the groups and again, no records in the datagridgroupheaders.
  9. Jeff
    Jeff avatar
    8 posts
    Member since:
    Jun 2016

    Posted 31 Aug Link to this post

    Ok for some reason, testing again, it worked the second time. I will test a few more times to see if its consistently working.
  10. Jeff
    Jeff avatar
    8 posts
    Member since:
    Jun 2016

    Posted 31 Aug in reply to Jeff Link to this post

    ok so the behavior is this...

    first load, it collapses the groups, then when I go to another page and then go back to that page as new, it doesn't collapse...so every other page load new it collapses them and then doesn't.
  11. Jeff
    Jeff avatar
    8 posts
    Member since:
    Jun 2016

    Posted 31 Aug Link to this post

    ok so I have nailed down exactly what is happening...

    1st load, doesn't have anything even with the wait, and does not close the groups.

    2nd load, (go to another page and then go back to the one with the grid and load the page new (not a frame.goback)) and then the page DOES close the groups.

    I added this command 'if (groupHeader.IsExpanded)' so that on the 3rd load it does not make the groups open again.

    So this still is a problem for the initial load, it doesn't show any headers and doesn't seem at the loaded time that it has rendered. So it does not collapse the groups on the first initial load of the page.
Back to Top