using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.WebControls;
using System.Data.OleDb;

public partial class _Default : System.Web.UI.Page
{
    protected void Button1_Click(object sender, EventArgs e)
    {
        int value = int.Parse(((sender as Button).NamingContainer.FindControl("TextBox1") as TextBox).Text);

        DataTable level1 = new DataTable();
        DataTable level2 = new DataTable();
        DataTable level3 = new DataTable();

        DataTable oneLevelUpTable = new DataTable();


        OleDbConnection MyOleDbConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("~/App_Data/Nwind.mdb"));

        OleDbDataAdapter level1Adapter = new OleDbDataAdapter();
        OleDbDataAdapter level2Adapter = new OleDbDataAdapter();
        OleDbDataAdapter level3Adapter = new OleDbDataAdapter();

        OleDbDataAdapter oneLevelUpAdapter = new OleDbDataAdapter();

        string level1Query, level2Query, level3Query;
        level1Query = "SELECT * FROM Customers WHERE TagID = " + value.ToString();
        level2Query = "SELECT * FROM Orders WHERE TagID = " + value.ToString();
        level3Query = "SELECT * FROM [Order Details] WHERE TagID = " + value.ToString();

        level1Adapter.SelectCommand = new OleDbCommand(level1Query, MyOleDbConnection);
        level2Adapter.SelectCommand = new OleDbCommand(level2Query, MyOleDbConnection);
        level3Adapter.SelectCommand = new OleDbCommand(level3Query, MyOleDbConnection);

        DataTable myDataTable = new DataTable();
        MyOleDbConnection.Open();
        try
        {
            level1Adapter.Fill(level1);
            level2Adapter.Fill(level2);
            level3Adapter.Fill(level3);
        }
        finally
        {
            //MyOleDbConnection.Close();
        }

        if (level1.Rows.Count > 0)
        {
            
            foreach (GridDataItem dataItem1 in RadGrid1.MasterTableView.Items)
            {
                if (dataItem1["TagID"].Text == value.ToString() && level2.Rows.Count==0)
                {                    
                    dataItem1.Selected = true;                    
                    controlID.Value = dataItem1.ClientID;
                    dataItem1.Focus();
                }
            }
        }
        else if (level2.Rows.Count > 0)
        {
            int index = 0;            
            foreach (GridDataItem dataItem2 in RadGrid1.MasterTableView.Items)
            {
                if (dataItem2["CustomerID"].Text == level2.Rows[0]["CustomerID"].ToString())
                {                                    
                    dataItem2.Expanded = true;                    
                    index = dataItem2.ItemIndex;
                    dataItem2.Focus();
                }
            }
            foreach (GridDataItem dataItem2_2 in RadGrid1.MasterTableView.Items[index].ChildItem.NestedTableViews[0].Items)
            {
                if (dataItem2_2["TagID"].Text == value.ToString())
                {
                    dataItem2_2.Selected = true;
                    controlID.Value = dataItem2_2.ClientID;
                    dataItem2_2.Focus();
                }
            }

        }
        else if (level3.Rows.Count > 0)
        {            
            int index = 0;
            int index2 = 0;
            string selectCommand = "SELECT * FROM Orders WHERE OrderID = " + level3.Rows[0]["OrderID"].ToString();
            oneLevelUpAdapter.SelectCommand = new OleDbCommand(selectCommand, MyOleDbConnection); ;
            oneLevelUpAdapter.Fill(oneLevelUpTable);

            foreach (GridDataItem dataItem1_1 in RadGrid1.MasterTableView.Items)
            {
                if (dataItem1_1["CustomerID"].Text == oneLevelUpTable.Rows[0]["CustomerID"].ToString())
                {
                    dataItem1_1.Expanded = true;
                    index = dataItem1_1.ItemIndex;
                }
            }

            foreach (GridDataItem dataItem2_2 in RadGrid1.MasterTableView.Items[index].ChildItem.NestedTableViews[0].Items)
            {
                if (dataItem2_2["OrderID"].Text == oneLevelUpTable.Rows[0]["OrderID"].ToString())
                {
                    dataItem2_2.Expanded = true;
                    index2 = dataItem2_2.ItemIndex;
                }
            }
            foreach (GridDataItem dataItem3_3 in RadGrid1.MasterTableView.Items[index].ChildItem.NestedTableViews[0].Items[index2].ChildItem.NestedTableViews[0].Items)
            {
                if (dataItem3_3["TagID"].Text == value.ToString())
                {
                    dataItem3_3.Selected = true;                    
                    controlID.Value = dataItem3_3.ClientID;
                    dataItem3_3.Focus();
                }
            }

        }

    }
    protected void Page_Load(object sender, EventArgs e)
    {
            
    }
    protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.ExpandCollapseCommandName)
        {   
            controlID.Value = e.Item.OwnerTableView.Items[e.Item.ItemIndex + 1].ClientID;
            e.Item.Focus();
        }       
    }

    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
        {
            GridEditFormItem editForm = (GridEditFormItem)e.Item;
            if (editForm.OwnerTableView.DataSourceID == "AccessDataSource3")
            {
                (editForm["UnitPrice"].Controls[0] as TextBox).Focus();
            }
            //IN CASE THERE ARE EDIT COLUMSN IN OTHER LEVELS
            //else if (editForm.OwnerTableView.DataSourceID == "AccessDataSource2")
            //{
            //    (editForm["OrderDate"].Controls[0] as TextBox).Focus();
            //}
            //else if (editForm.OwnerTableView.DataSourceID == "AccessDataSource1")
            //{
            //    (editForm["CompanyName"].Controls[0] as TextBox).Focus();
            //}
        }
    }

}
