Expand All button in Group Header?

5 posts, 1 answers
  1. neebs
    neebs avatar
    106 posts
    Member since:
    Apr 2007

    Posted 29 Dec 2009 Link to this post

    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. 


  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 29 Dec 2009 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. neebs
    neebs avatar
    106 posts
    Member since:
    Apr 2007

    Posted 30 Dec 2009 Link to this post

    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
  5. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 31 Dec 2009 Link to this post

    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.
  6. neebs
    neebs avatar
    106 posts
    Member since:
    Apr 2007

    Posted 01 Jan 2010 Link to this post

    Awesome! Worked like a charm.

    Thanks
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017