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

[Solved] ItemIndexHierarchical Removal Problem

2 Answers 161 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Andrew
Top achievements
Rank 1
Andrew asked on 06 Jul 2009, 03:25 PM
Hi

I have 2 questions/problems to ask: 

1) I have a Hierarchical Grid that drills down 3 levels and I am currently using a snippet of code found on the forum (see below) that allows me to keep open any children, grandchildren within the Hierarchical Grid on rebind. Although it only works for 2 levels , is it possible for this to be modified for, say, an infinite amount ot children, grandchildren and so on...

 
        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.Items[key].Expanded = true;  
                }  
            }  
 
            //Select all items using our custom storage  
            indexes = new string[this.SelectedStates.Keys.Count];  
            this.SelectedStates.Keys.CopyTo(indexes, 0);  
 
            arr = new ArrayList(indexes);  
            //Sort to ensure that a parent item is selected before any of its children  
            arr.Sort();  
 
            foreach (string key in arr)  
            {  
                bool value = (bool)this.SelectedStates[key];  
                if (value)  
                {  
                    RadGrid1.Items[key].Selected = true;  
                }  
            }  
        }  
 
        //Save/load expanded states Hash from the session  
        //this can also be implemented in the ViewState  
        private Hashtable ExpandedStates  
        {  
            get  
            {  
                if (this._visitsExpandedState == null)  
                {  
                    _visitsExpandedState = this.Session["_visitsExpandedState"] as Hashtable;  
                    if (_visitsExpandedState == null)  
                    {  
                        _visitsExpandedState = new Hashtable();  
                        this.Session["_visitsExpandedState"] = _visitsExpandedState;  
                    }  
                }  
 
                return this._visitsExpandedState;  
            }  
        }  
 
        //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.StartsWith(parentHierarchicalIndex + "_") ||  
                    index.StartsWith(parentHierarchicalIndex + ":"))  
                {  
                    this.ExpandedStates.Remove(index);  
                }  
            }  
        }  
 
        //Save/load selected states Hash from the session  
        //this can also be implemented in the ViewState  
        private Hashtable SelectedStates  
        {  
            get  
            {  
                if (this._selectedState == null)  
                {  
                    _selectedState = this.Session["_selectedState"] as Hashtable;  
                    if (_selectedState == null)  
                    {  
                        _selectedState = new Hashtable();  
                        this.Session["_selectedState"] = _selectedState;  
                    }  
                }  
 
                return this._selectedState;  
            }  
        }  
 
        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.ItemIndexHierarchical] = true;  
                }  
                else //collapsed  
                {  
                    this.ExpandedStates.Remove(e.Item.ItemIndexHierarchical);  
                    this.ClearExpandedChildren(e.Item.ItemIndexHierarchical);  
                }  
            }  
            //Is the item about to be selected   
            else if (e.CommandName == RadGrid.SelectCommandName)  
            {  
                //Save its unique index among all the items in the hierarchy  
                this.SelectedStates[e.Item.ItemIndexHierarchical] = true;  
            }  
            //Is the item about to be deselected   
            else if (e.CommandName == RadGrid.DeselectCommandName)  
            {  
                this.SelectedStates.Remove(e.Item.ItemIndexHierarchical);  
            }  
        }  
 

2) Within the same Hierarchical Grid I am allowing users to remove either children and grandchildren , and when doing so in order for the ribind to reopen any previously open levels, i need to remove the ExpandedStates.Keys of the item being deleted, but i am having trouble doing so, and this causes my rebind to error due to it finding an expandedState Key that no longer exists.

Can you help?

Thanks in advance

2 Answers, 1 is accepted

Sort by
0
Tsvetoslav
Telerik team
answered on 09 Jul 2009, 12:45 PM
Hello Greg,

I have prepared a small sample that demonstrates this scenario using both session and cookies to cache the expanded items' keys.

All the best,
Tsvetoslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Andrew
Top achievements
Rank 1
answered on 17 Jul 2009, 09:57 AM
Hi

That demo you sent me really helped, its all working as required now.

Thanks for all your help.
Tags
Grid
Asked by
Andrew
Top achievements
Rank 1
Answers by
Tsvetoslav
Telerik team
Andrew
Top achievements
Rank 1
Share this question
or