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

UWP DataGrid Collapse All Groups

10 Answers 143 Views
DataGrid
This is a migrated thread and some comments may be shown as answers.
Jeff
Top achievements
Rank 1
Jeff asked on 22 Aug 2017, 07:41 PM

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?

10 Answers, 1 is accepted

Sort by
0
Jeff
Top achievements
Rank 1
answered on 23 Aug 2017, 05:11 PM
Does Telerik monitor these forums?
0
Stefan Nenchev
Telerik team
answered on 25 Aug 2017, 08:31 AM
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
0
Jeff
Top achievements
Rank 1
answered on 25 Aug 2017, 05:14 PM

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;

0
Stefan Nenchev
Telerik team
answered on 28 Aug 2017, 10:18 AM
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
0
Jeff
Top achievements
Rank 1
answered on 28 Aug 2017, 06:02 PM
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?
0
Stefan Nenchev
Telerik team
answered on 31 Aug 2017, 10:25 AM
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
0
Jeff
Top achievements
Rank 1
answered on 31 Aug 2017, 04:09 PM
So, it actually ends up hitting this code twice, but it doesn't collapse the groups and again, no records in the datagridgroupheaders.
0
Jeff
Top achievements
Rank 1
answered on 31 Aug 2017, 04:13 PM
Ok for some reason, testing again, it worked the second time. I will test a few more times to see if its consistently working.
0
Jeff
Top achievements
Rank 1
answered on 31 Aug 2017, 04:25 PM
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.
0
Jeff
Top achievements
Rank 1
answered on 31 Aug 2017, 05:03 PM
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.
Tags
DataGrid
Asked by
Jeff
Top achievements
Rank 1
Answers by
Jeff
Top achievements
Rank 1
Stefan Nenchev
Telerik team
Share this question
or