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...
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
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