RadGridView: Expand / Collapse all the groups which have the same level from a Context Menu item click

2 Answers 1320 Views
GridView
minh
Top achievements
Rank 2
Iron
Iron
Iron
minh asked on 15 Jul 2022, 12:12 PM

Dear Teleriker,

I have a grid using RadGridView with grouping like below image.

I developed a context menu based on the GridViewHeaderCell.

Actually, I would have the behavior like that: When I click on an item of Context Menu (Expand All or Collapse All), it will expand/collapse only the sub-groups which are under the same group.

For example, in the image below, as I'm currently on the group "Cash" of group parent "Asset...Pictet & Cie (Suisse)", I expect that it will close all the groups under the group parent "Asset...Pictet & Cie (Suisse)" (like:Cash, Share, Bonds...) but let expanded other groups (like: group "Cash" under group parent "Asset...First Americain National Bank".

 

 

Is there any solution for that?

Many thanks for your help,

Minh

Stenly
Telerik team
commented on 20 Jul 2022, 11:51 AM

I am currently reviewing this requirement and will get back to you in a couple of hours.

2 Answers, 1 is accepted

Sort by
0
Accepted
Stenly
Telerik team
answered on 20 Jul 2022, 12:42 PM

Hello Minh Vu,

To achieve this requirement, you could utilize the GetClickedElement method of the RadContextMenu control and get the element with the type of GroupHeaderRow (if GroupRenderMode is set to Flat). Then, find if the clicked header row has a parent group, which could be done through its Group.ParentGroup property (if this property is null it would mean that this group has no group parent). If it has no parent, meaning it would be the topmost group, call the Expand or Collapse method of the RadGridView instance and pass its Group property as a parameter. If it has a parent, cast its Group.ParentGroup property to a type of QueryableCollectionViewGroup and iterate the Subgroups collection (this collection contains all of the sub-groups of this parent group). then, call the Expand or Collapse method on the current sub-group. This logic could be executed in the ItemClick event of the RadContextMenu control.

The following code snippets show this suggestion's implementation:

private void RadMenuItem_Click(object sender, Telerik.Windows.RadRoutedEventArgs e)
{
    var menu = (RadContextMenu)sender;
    var menuItem = (RadMenuItem)e.OriginalSource;

    var keyWord = menuItem.Header.ToString();

    var groupHeaderRow = menu.GetClickedElement<GroupHeaderRow>();
    if (groupHeaderRow != null)
    {
        var parentGroupHeaderRow = groupHeaderRow.Group.ParentGroup as QueryableCollectionViewGroup;
        //It has a parent group
        if (parentGroupHeaderRow != null)
        {
            foreach (var subgroup in parentGroupHeaderRow.Subgroups)
            {
                this.PerformExpandCollapse(keyWord, subgroup);
            }
        }
        //It does not have a parent group
        else
        {
            this.PerformExpandCollapse(keyWord, groupHeaderRow.Group);
        }
    }
}

The implementation of the PerformExpandCollapse method is as follows:

private void PerformExpandCollapse(string keyWord, IGroup group)
{
    if (keyWord.Contains("Expand"))
    {
        this.gridView.ExpandGroup(group);
    }
    else
    {
        this.gridView.CollapseGroup(group);
    }

}

The implementation will produce this result:

I have attached a sample project for you to test.

Regards,
Stenly
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/.

0
minh
Top achievements
Rank 2
Iron
Iron
Iron
answered on 21 Jul 2022, 04:47 AM

Hi Stenly,

Your solution work like a charm!

Many thanks for your help that helped us saving a lot of tries and efforts.

Have a great day

Tags
GridView
Asked by
minh
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Stenly
Telerik team
minh
Top achievements
Rank 2
Iron
Iron
Iron
Share this question
or