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

Expand All button in Group Header?

4 Answers 222 Views
Grid
This is a migrated thread and some comments may be shown as answers.
neebs
Top achievements
Rank 2
neebs asked on 30 Dec 2009, 01:16 AM
Is there an example of adding an expand all / collapse all button in a group header? I have looked at the example here:


but this is for a hierarchical grid. I'm interested in such a feature for a grid that is grouped, with the button in the group header. 


4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 30 Dec 2009, 05:26 AM
Hello,

I tried following code in order to expand/collapse button in groups header.

CS:
 
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridGroupHeaderItem) 
        { 
            GridGroupHeaderItem GroupHeader = (GridGroupHeaderItem)e.Item; 
            Button btn = new Button(); 
            btn.Text = "Expand/Collapse"
            btn.Click += new EventHandler(btn_Click); 
            GroupHeader.DataCell.Controls.Add(btn); 
        } 
    } 
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridGroupHeaderItem) 
        { 
            GridGroupHeaderItem GroupHeader = (GridGroupHeaderItem)e.Item; 
            Button btn = new Button(); 
            btn.Text = "Expand/Collapse"
            GroupHeader.DataCell.Controls.Add(btn); 
        } 
    } 
 
    void btn_Click(object sender, EventArgs e) 
    { 
        Button btn = (Button)sender; 
        GridGroupHeaderItem GroupHeader = (GridGroupHeaderItem) btn.NamingContainer; 
        GroupHeader.Expanded = !GroupHeader.Expanded; 
    } 

-Shinu.
0
neebs
Top achievements
Rank 2
answered on 30 Dec 2009, 04:21 PM

I'm sorry, I should have proof-read my original post. The functionality I'm looking for, which is implemented in the example referred to in that post for a hierarchical grid, is an Expand/Collapse All button in the Grid Header. In my original post, I incorrectly said "Group Header". Specifically in the attached image, I would like a button in the cell to the left of the header cell that reads "Manager", which when clicked would expand all groups or collapse all groups.

Note: I have already added code that creates that button in the RadGrid's PreRender event handler and it looks good, however I am unable to make use of the button as it is created after the page_load method and I believe I am unable to dynamically create a button at that point in the page refresh and have it fire. So when I click that button, the Page_Load event fires, but not the event that is attached to the button's OnClick method itself.

See my code below:

                        Button toggle = new Button();  
                        toggle.ID = "ButtonExpandCollapse";  
                        toggle.CssClass = "rgExpand";  
                        toggle.CommandName = "ExpandAll";  
                        toggle.UseSubmitBehavior = true;  
                        toggle.Click += new EventHandler(toggle_Click);  
                        toggle.EnableViewState = true;  
                        cell.Controls.Add(toggle);  
 
                        AjaxSetting ajaxsetting = new AjaxSetting(toggle.ID);  
                        ajaxsetting.UpdatedControls.Add(new AjaxUpdatedControl(RadGridRealMcCoy.ID, RadAjaxLoadingPanelRealMCoy.ID));  
                        RadAjaxManagerRealMcCoy.AjaxSettings.Add(ajaxsetting);  
 

It would be sufficient if when the Page_Load event fires I could determine which control caused it to fire, and handle it accordingly, but I don't see where that's possible. Also,  I could probably get away with placing an expand/collapse all button somewhere on the form, but I think the intuitive place for it to be is in the upper left hand corner of the grid itself.

Thanks, Steve
0
Accepted
Princy
Top achievements
Rank 2
answered on 31 Dec 2009, 01:02 PM
Hello Steve,

For the Click event of the button to get triggered try adding the button in the ItemCreated event rather than using the PreRender event. Check out the following code:
c#:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridHeaderItem) 
        { 
            GridHeaderItem header = (GridHeaderItem)e.Item; 
            Button toggle = new Button(); 
            toggle.ID = "ButtonExpandCollapse"
            toggle.CssClass = "rgExpand";             
            toggle.UseSubmitBehavior = true
            toggle.Click += new EventHandler(toggle_Click); 
            toggle.EnableViewState = true
            header["ExpandColumn"].Controls.Add(toggle);         
        } 
    } 
 
    void toggle_Click(object sender, EventArgs e) 
    { 
        RadGrid1.MasterTableView.HierarchyDefaultExpanded = !RadGrid1.MasterTableView.HierarchyDefaultExpanded; 
        RadGrid1.Rebind(); 
    } 
Hope this helps...

Happy New Year!
-Princy.
0
neebs
Top achievements
Rank 2
answered on 01 Jan 2010, 02:56 PM
Awesome! Worked like a charm.

Thanks
Tags
Grid
Asked by
neebs
Top achievements
Rank 2
Answers by
Shinu
Top achievements
Rank 2
neebs
Top achievements
Rank 2
Princy
Top achievements
Rank 2
Share this question
or