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

Problem with Single Group Expand/Collapse

5 Answers 389 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Johann Herunter
Top achievements
Rank 1
Johann Herunter asked on 26 Jan 2010, 01:47 PM
Hello,

we have changed the Grid behaviour to support a "Single Group Expand/Collapse" mechanism. The property "GroupsDefaultExpanded" is set to false on the MasterTableView. Paging is set to false by default, the PageSize is set to 15 by default.
The DataSource contains 150 items, which are configured to build 5 groups.

When a group is expanded, the ItemCommand event handler is called, which collapses all group headers and expands the currently selected. AllowPaging is set to true and then "Rebind()" is called.

Consider the following scenario:
1.) The page is initially called.
2.) The DataSource is added in the NeedDataSource event.
3.) The Grid is build with 5 groups, no paging enabled, all is fine.

4.) The third group expand button is clicked.
5.) The ItemCommand event handler gets called and processes the steps as defined above.
6.) The NeedDataSource event handler gets called and retrieved the Items based on the selected group header. (This data is transfered from the ItemCommand event handler). The DataSource contains now 34 entries. 4 just to build the groups, and 30 detail entries for the group to expand.
7.) The ItemCreated and ItemDataBound event handler gets called.


Result:
The page now displays 3 Groups, the second with an expanded image button, but no items in the group. The paging bar contains figures for the three pages. Now I click on the second page, back to the first page.
After that, the first page is displayed as expected: The first and second Groups are collapsed, the third is openned and displays the items.


When now another group (say the second) is openned, the same happens: The first group is initial collapsed, the second expanded, but no items in it. After using paging, the page looks like expected.

Could you please help me to determine the issue, why group items are displayed only after a paging event? Is there some call missing?

Following is some Code from the ItemCommand event handler:
if (e.CommandName == RadGrid.ExpandCollapseCommandName) 
            { 
                GridGroupHeaderItem groupHeaderItem = (GridGroupHeaderItem) e.Item; 
                 
                GridItem[] groupHeaderItems = grid.MasterTableView.GetItems(GridItemType.GroupHeader);  
                // collapse all group header items currently on the page to support a "single open group" effect  
                foreach (GridGroupHeaderItem grpHeaderItem in groupHeaderItems) 
                { 
                    grpHeaderItem.Expanded = false;  
                } 
 
                // When "Expand" is clicked, the expanded state is set to false. Therefore, the division from this group header  
                // needs to be read. When "Collapse" is clicked, the state is assigned "true"  
                if (!groupHeaderItem.Expanded) 
                { 
                    string currentDivision = this.GetDivisionFromGroupHeader(groupHeaderItem); 
                    new ObjectLifecycleHelper(this.Page, this.ViewState).SetCurrentSelectedGridDivision(currentDivision); 
                     
                    groupHeaderItem.Expanded = true
                } 
 
                if (!grid.AllowPaging) 
                { 
                    grid.AllowPaging = true
                    grid.PageSize = 15;  
                } 
                // rebind in each case, because otherwise the Expanded/Collapsed state is not changed by the view  
                grid.Rebind(); 



Thanks for your support!

Sincereley,
Johann

5 Answers, 1 is accepted

Sort by
0
Radoslav
Telerik team
answered on 29 Jan 2010, 01:31 PM
Hello Johann,

When the GroupsDefaultExpanded property is set to false by default, every time when you rebind the grid all items will be collapsed. So the issue in your case is caused by calling the Rebind method in the ItemCommand event handler. To avoid the undesired behavior you could do the following changes:

Remove GroupsDefaultExpanded property. Then in the Page_Load event handler, on initial page load collapse all items:

protected void Page_Load(object sender, EventArgs e)
  {
      if (!Page.IsPostBack)
      {
          RadGrid1.Rebind();
          CollapseAll();
      }
  }
 
  private void CollapseAll()
  {
      foreach (GridItem item in RadGrid1.MasterTableView.Controls[0].Controls)
      {
          if (item is GridGroupHeaderItem)
          {
              item.Expanded = false;
          }
      }
  }


You could get additional information about how to collapse all items on grouping in this online documentation article:

http://www.telerik.com/help/aspnet-ajax/grdcollapseallitemsongrouping.html

Then in the ItemCommand event handler you could expand the selected Item and enable the grid paging. In order to achieve it you need to cancel the command as below:

void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.ExpandCollapseCommandName)
        {
            GridGroupHeaderItem groupHeaderItem = (GridGroupHeaderItem)e.Item;
 
            e.Canceled = true;
 
            CollapseAll();
 
            if (!groupHeaderItem.Expanded)
            {
                groupHeaderItem.Expanded = true;
            }
 
            if (!RadGrid1.AllowPaging)
            {
                RadGrid1.AllowPaging = true;
                RadGrid1.PageSize = 15;
            }
             
            RadGrid1.Rebind();
        }
    }


Additionally I am sending you a simple example which illustrates how to achieve the desired scenario.

I hope this helps.

Kind regards,
Radoslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Johann Herunter
Top achievements
Rank 1
answered on 29 Jan 2010, 06:53 PM
Hello Radoslav,

thanks for the information and solution you send me!

Unfortunately, the sample doesn't work as expected. When first openned, the groups are collapsed, all is fine. But when I click on any group, always the first collapses with all other too. (This can be reproduced easily by commenting out the "AllowPaging=true" property.
In that case, ALL groups are expanded at once. But single group collapse/expand is something we MUST support.


We cannot use easily the OnInit event because of our page and logic structure. So all initialization is done in the Page_Load event when the Page is NOT in PostBack. Is the OnInit event really needed for these assignments? Does the Grid behave differently when assignment at this place?

When using your sample in my project, the result is that the group to expand is not expanded at all, even when double checking that the group header "Expanded" property is set. What could I do wrong?

You declared the Expressions declaratively. But we need to set them always at runtime based on some criteria. Currently, we do this in Page_Load when NOT in Postback. Is this the right place? Can this be an issue of our problems?


Regards,
Johann
0
Radoslav
Telerik team
answered on 03 Feb 2010, 03:00 PM
Hello Johann,

If you want to enable paging programmatically you need to rebind RadGrid, however in your case when RadGrid is rebound all items lose theirs collapsed state, because the state is set programmatically too.

In order to progress in the resolution of this matter I will ask you to send us a simple runnable application demonstrating the issues. You could open a formal support ticket from your Telerik account and attach a ZIP file there. In that way we can reproduce and pinpoint the problems you're facing on our side, understand the logic of your application and provide a solution.

Regarding your last question, the approach which you use (setting expressions in Page_Load), is right. Also you could check out this online documentation article which demonstrates how to set the GroupByExpressions property in the code-behind:

http://www.telerik.com/help/aspnet-ajax/grdgroupbyprogrammaticdefinition.html

Regards,
Radoslav
the Telerik team

Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Follow the status of features or bugs in PITS and vote for them to affect their priority.
0
Johann Herunter
Top achievements
Rank 1
answered on 10 Feb 2010, 12:50 PM
Hello Radoslav ,

thanks for your support! After some hard time to get it working, the solution was NOT to Expand the group header in the ItemCommand event handler. The ItemCommand event handler just caches the current group and calculates, which group headers should be openned. The grouping information is later assigned in the ItemDataBound event handler, which works very good!

One of the major problems is that the RadGrid changes the UniqueID as well as the GroupIndex of the Group Headers after paging. This makes it rather difficult to determine which group should be openned on the next page.


Maybe it is possible in a future release, that the UniqueID of a Group Header with the same data stays the same even when using paging? Or some new property, which gets this information assigned. This would make life much more easier.

Regards
Johann
0
Radoslav
Telerik team
answered on 12 Feb 2010, 03:47 PM
Hello Johann,

I am glad that you achieved the desired functionality. Also with respect to your suggestion concerning the UniqueID of a Group Header -  I will forward it to our development department to be considered further.

Regards,
Radoslav
the Telerik team

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 Public Issue Tracking system and vote to affect the priority of the items.
Tags
Grid
Asked by
Johann Herunter
Top achievements
Rank 1
Answers by
Radoslav
Telerik team
Johann Herunter
Top achievements
Rank 1
Share this question
or