How to rebind a grouping RadGrid without collapsing the items

9 posts, 0 answers
  1. Herman Gouw
    Herman Gouw avatar
    100 posts
    Member since:
    Aug 2012

    Posted 12 Oct 2010 Link to this post

    Hi,

    In my current project I have a RadGrid with multilevel grouping of items.

    The items displayed on the RadGrid are stored in a DataSet.

    When any of items in the DataSet changes (because of some operations on the web page), I redisplay the RadGrid as follows:

    this.RadGrid.DataSource = DataSet;
    this.RadGrid.DataBind();

    However, this causes all the RadGrid items to be collapsed (if any of them has been expanded).

    Can you please show me how to redisplay the updated DataSet on the RadGrid and at the same time preserving the expanded items?

    Please note that all operations need to be done on the server (instead of client)?

    Regards,
    Herman
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 12 Oct 2010 Link to this post

    Hi Herman,


    Normally when you rebind a  hierarchical grid the settings/expanded state for the nested tables will be lost.

    You can refer the following Code Library which illustrates how to retain the expanded/selected state for grid items in hierarchy upon a rebind command.
    Retain expanded/selected state in hierarchy on rebind


    -Shinu.
  3. Herman Gouw
    Herman Gouw avatar
    100 posts
    Member since:
    Aug 2012

    Posted 13 Oct 2010 Link to this post

    Hi Shinu,

    Thank you for your reply.

    I have looked closely at the Code Library samples that you mentioned  (in particular the one given in the zip file 218635_retainexpandselectstateinhierarchyonrebind-ajax-2009-1.zip).

    The solution works only for Hierarchical RadGrid but not for Grouping RadGrid.

    I tried to modify the solution in RadGrid1_ItemCommand event handler  as follows:

    protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.ExpandCollapseCommandName)
        {
            if (!e.Item.Expanded)
            {
                this.ExpandedStates[e.Item.GroupIndex] = true;
            }
            else
            {
                this.ExpandedStates.Remove(e.Item.GroupIndex);
                this.ClearExpandedChildren(e.Item.GroupIndex);
            }
        }
    }

    but I still got the following error message Grouping Index Error

    Can you please help me?

    Thanks,
    Herman
  4. Herman Gouw
    Herman Gouw avatar
    100 posts
    Member since:
    Aug 2012

    Posted 14 Oct 2010 Link to this post

    Hi Shinu,

    Can you please help me in modifying the sample (that you gave me) to work with Grouping RadGrid?

    Please see my comments in my previous reply.

    Thanks,
    Herman
  5. Herman Gouw
    Herman Gouw avatar
    100 posts
    Member since:
    Aug 2012

    Posted 17 Oct 2010 Link to this post

    Hi,

    Can someone please help me finding the solution to my problem?

    Thanks,
    Herman
  6. Dimo
    Admin
    Dimo avatar
    8443 posts

    Posted 20 Oct 2010 Link to this post

    Hi Herman,

    The code behind requires some more changes.

    1. Use e.Item.GroupIndex instead of e.Item.ItemIndexHierarchical. Both are of string type.


    2. Instead of

    if (index.StartsWith(parentHierarchicalIndex + "_") || index.StartsWith(parentHierarchicalIndex + ":"))

    you should use

    if (index == parentHierarchicalIndex)


    3. Instead of

    RadGrid1.Items[key].Expanded = true;

    you should use

    RadGrid1.MasterTableView.GetItems(GridItemType.GroupHeader)[int.Parse(key)].Expanded = true;

    This is because RadGrid.Items contains data items only, while you are interested in GroupHeaders now.


    4. Depending on whether RadGrid.MasterTableView.GroupsDefaultExpanded is true or false, you should store and restore collapsed or expanded group headers. The code below is for the case when GroupsDefaultExpanded="false"


    5. The code below does not take into account paging and will not work correctly if you enable paging, because GroupIndexes are unique for the current page only. When using paging, you will have to store the GroupIndex together with the page number.



    public partial class _Default : System.Web.UI.Page
    {
        private Hashtable _ordersExpandedState;
     
        public void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //reset states
                this._ordersExpandedState = null;
                this.Session["_ordersExpandedState"] = null;
            }
        }
        //Save/load expanded states Hash from the session
        //this can also be implemented in the ViewState
        private Hashtable ExpandedStates
        {
            get
            {
                if (this._ordersExpandedState == null)
                {
                    _ordersExpandedState = this.Session["_ordersExpandedState"] as Hashtable;
                    if (_ordersExpandedState == null)
                    {
                        _ordersExpandedState = new Hashtable();
                        this.Session["_ordersExpandedState"] = _ordersExpandedState;
                    }
                }
     
                return this._ordersExpandedState;
            }
        }
     
        //Clear the state for all expanded children if a parent item is collapsed
        private void ClearExpandedChildren(string parentHierarchicalIndex)
        {
            string[] indexes = new string[this.ExpandedStates.Keys.Count];
            this.ExpandedStates.Keys.CopyTo(indexes, 0);
            foreach (string index in indexes)
            {
                //all indexes of child items
                if (index == parentHierarchicalIndex)
                {
                    this.ExpandedStates.Remove(index);
                }
            }
        }
         
        protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
        {
            //save the expanded/selected state in the session
            if (e.CommandName == RadGrid.ExpandCollapseCommandName)
            {
                //Is the item about to be expanded or collapsed
                if (!e.Item.Expanded)
                {
                    //Save its unique index among all the items in the hierarchy
                    this.ExpandedStates[e.Item.GroupIndex] = true;
                }
                else //collapsed
                {
                    this.ExpandedStates.Remove(e.Item.GroupIndex);
                    this.ClearExpandedChildren(e.Item.GroupIndex);
                }
            }
        }
        protected void RadGrid1_DataBound(object sender, EventArgs e)
        {
            //Expand all items using our custom storage
            string[] indexes = new string[this.ExpandedStates.Keys.Count];
            this.ExpandedStates.Keys.CopyTo(indexes, 0);
     
            ArrayList arr = new ArrayList(indexes);
            //Sort so we can guarantee that a parent item is expanded before any of
            //its children
            arr.Sort();
     
            foreach (string key in arr)
            {
                bool value = (bool)this.ExpandedStates[key];
                if (value)
                {
                    RadGrid1.MasterTableView.GetItems(GridItemType.GroupHeader)[int.Parse(key)].Expanded = true;
                }
            }
        }
    }


    Best wishes,
    Dimo
    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
  7. Herman Gouw
    Herman Gouw avatar
    100 posts
    Member since:
    Aug 2012

    Posted 21 Oct 2010 Link to this post

    Hi Dimo,

    Thank you for your reply.

    I have tried your solution but it still does not work with Multi Level Grouping RadGrid.

    Here are the screen shots :

    1. When the RadGrid was initially displayed

    2. After the first GroupHeader and Item have been expanded

    3. The error message displayed after rebinding the RadGrid

    Regards,
    Herman
  8. Dimo
    Admin
    Dimo avatar
    8443 posts

    Posted 21 Oct 2010 Link to this post

    Hello Herman,

    Have you tried some debugging? The GroupIndex of nested group headers consists of several numbers, for example:

    "2_0"

    This should not be cast to an integer, because you will lose the information that this is a nested header.

    So you should modify the code a little:

    foreach (GridItem item in RadGrid1.MasterTableView.GetItems(GridItemType.GroupHeader))
    {
        if (item.GroupIndex == key)
            item.Expanded = true;
    }

    instead of

    RadGrid1.MasterTableView.GetItems(GridItemType.GroupHeader)[int.Parse(key)].Expanded = true;


    Here is the full DataBound event handler:

    protected void RadGrid1_DataBound(object sender, EventArgs e)
    {
        //Expand all items using our custom storage
        string[] indexes = new string[this.ExpandedStates.Keys.Count];
        this.ExpandedStates.Keys.CopyTo(indexes, 0);
     
        ArrayList arr = new ArrayList(indexes);
        //Sort so we can guarantee that a parent item is expanded before any of
        //its children
        arr.Sort();
     
        foreach (string key in arr)
        {
            bool value = (bool)this.ExpandedStates[key];
            if (value)
            {
                foreach (GridItem item in RadGrid1.MasterTableView.GetItems(GridItemType.GroupHeader))
                {
                    if (item.GroupIndex == key)
                        item.Expanded = true;
                }
                //RadGrid1.MasterTableView.GetItems(GridItemType.GroupHeader)[int.Parse(key)].Expanded = true;
            }
        }
    }



    All the best,
    Dimo
    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
  9. Herman Gouw
    Herman Gouw avatar
    100 posts
    Member since:
    Aug 2012

    Posted 21 Oct 2010 Link to this post

    I have already done some debugging and tried a few things.

    You were right, on the screen shots that I gave you, the e.Item.GroupIndex of the expanded items were "0" and "0_0".

    However, in RadGrid1_DataBound in order to correctly expand the appropriate items,

    somehow the string "0" and "0_0" need to be converted to integer 0 and 1. i.e.

    RadGrid1.MasterTableView.GetItems(GridItemType.GroupHeader)[0].Expanded = true;
    RadGrid1.MasterTableView.GetItems(GridItemType.GroupHeader)[1].Expanded = true;

    And I couldn't figure out how to do this.

    Regards,
    Herman
Back to Top