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

How to rebind a grouping RadGrid without collapsing the items

8 Answers 489 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Herman Gouw
Top achievements
Rank 2
Herman Gouw asked on 12 Oct 2010, 06:04 AM
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

8 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 12 Oct 2010, 08:42 AM
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.
0
Herman Gouw
Top achievements
Rank 2
answered on 14 Oct 2010, 02:25 AM
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
0
Herman Gouw
Top achievements
Rank 2
answered on 15 Oct 2010, 01:56 AM
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
0
Herman Gouw
Top achievements
Rank 2
answered on 18 Oct 2010, 02:37 AM
Hi,

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

Thanks,
Herman
0
Dimo
Telerik team
answered on 20 Oct 2010, 07:26 AM
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
0
Herman Gouw
Top achievements
Rank 2
answered on 21 Oct 2010, 06:33 AM
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
0
Dimo
Telerik team
answered on 21 Oct 2010, 07:01 AM
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
0
Herman Gouw
Top achievements
Rank 2
answered on 21 Oct 2010, 07:49 AM
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
Tags
Grid
Asked by
Herman Gouw
Top achievements
Rank 2
Answers by
Shinu
Top achievements
Rank 2
Herman Gouw
Top achievements
Rank 2
Dimo
Telerik team
Share this question
or