using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Telerik.Web.UI;
using System.Collections;
using System.Text.RegularExpressions;

public partial class _Default : System.Web.UI.Page
{

  private Hashtable _ordersExpandedState;

  protected void Page_Load(object sender, EventArgs e)
  {

  }
  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;
    }
  }
  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
        ExpandedStates.Add(ConstructHashKey((GridDataItem)e.Item), true);
      }
      else //collapsed
      {
        ExpandedStates.Remove(ConstructHashKey((GridDataItem)e.Item));
        ClearExpandedChildren(e.Item.ItemIndexHierarchical);

      }
    }
    else if (e.CommandName == RadGrid.DeleteCommandName)
    {

      if (ExpandedStates.ContainsKey(ConstructHashKey((GridDataItem)e.Item)))
      {
        ExpandedStates.Remove(ConstructHashKey((GridDataItem)e.Item));
        ClearExpandedChildren(e.Item.ItemIndexHierarchical);
      }

    }
  }

  private void ClearExpandedChildren(string parentHierarchicalIndex)
  {

    string index = "";
    foreach (GridDataItem item in RadGrid1.Items)
    {
      index = item.ItemIndexHierarchical;
      if (index.StartsWith(parentHierarchicalIndex + "_") || index.StartsWith(parentHierarchicalIndex + ":"))
      {
        ExpandedStates.Remove(ConstructHashKey((GridDataItem)item));
      }
    }

  }

  protected void RadGrid1_DataBound(object sender, EventArgs e)
  {

    //Expand all items using our custom storage
    string[] keys = new string[this.ExpandedStates.Keys.Count];
    this.ExpandedStates.Keys.CopyTo(keys, 0);
    
    ArrayList arr = new ArrayList(keys);

    arr.Sort();

    foreach (string key in arr)
    {
      bool value = (bool)this.ExpandedStates[key];
      if (value)
      {
        GridDataItem item = GetItemByDataKey(key);

        if (item != null)
        {
          item.Expanded = true;
        }
      }
    }

  }

  protected GridDataItem GetItemByDataKey(string dataKey)
  {

    foreach (GridDataItem item in RadGrid1.Items)
    {
      if (ConstructHashKey(item) == dataKey)
      {
        return item;
      }
    }

    return null;
  }

  protected string ConstructHashKey(GridDataItem item)
  {
    string hashKey = "";
    int i = 0;

    hashKey = item.OwnerTableView.HierarchyIndex + "_";

    foreach (string dataKeyName in item.OwnerTableView.DataKeyNames)
    {
      if (i != 0)
        hashKey += "_";

      hashKey += item.GetDataKeyValue(dataKeyName);
    }

    return hashKey;
  }

  protected void RadGrid1_ItemDeleted(object source, GridDeletedEventArgs e)
  {
    RadGrid1.Rebind();
  }
}