using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Telerik.Web.UI;
using System.Collections;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e)
    {
        if (e.Item.OwnerTableView.Name == "OrderDetails")
        {
            //Cancel the default automatic operation and perform manual update.
            e.Canceled = true;

            //Optional - store the parrent item hierarchical index. Use it later to keep the expanded status of the level 3 table parent item.
            Session["ExpandedItem"] = e.Item.OwnerTableView.ParentItem.ItemIndexHierarchical;

            //Update the level 3 table
            GridEditableItem editedItem = e.Item as GridEditableItem;
            Hashtable newValues = new Hashtable();
            e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
            foreach (DictionaryEntry entry in newValues)
            {
                SqlDataSource3.UpdateParameters[entry.Key.ToString()].DefaultValue = entry.Value.ToString();
            }
            SqlDataSource3.Update();
            editedItem.Edit = false;
             
            //Optional - for demonstration purposes the parent item of the level 3 table is updated.
            SqlDataSource parentSource = new SqlDataSource();
            parentSource.ConnectionString = SqlDataSource2.ConnectionString;
            parentSource.UpdateCommand = "UPDATE [Orders] SET [OrderDate] = @OrderDate WHERE [OrderID] = @OrderID";
            parentSource.UpdateParameters.Add(new Parameter("OrderID", DbType.Int32, e.Item.OwnerTableView.ParentItem.GetDataKeyValue("OrderID").ToString()));
            parentSource.UpdateParameters.Add(new Parameter("OrderDate", DbType.DateTime, DateTime.Now.AddYears(-15).ToString()));
            parentSource.Update();

            //Update the level 2 table. 
            e.Item.OwnerTableView.ParentItem.OwnerTableView.Rebind();
        }
    }

   
    protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
        //Optional - keep the expanded status of the updated parent item.
        foreach (GridDataItem item in RadGrid1.Items)
        {
            if (Session["ExpandedItem"] != null && item.ItemIndexHierarchical == Session["ExpandedItem"].ToString())
            {
                item.Expanded = true;
                Session["ExpandedItem"] = null;
            }
        }
    }
}
