using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using Telerik.Web.UI;

namespace WebApplication2
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (RadGrid1.SelectedItems.Count > 0)
            {

            }
        }

        protected void lbPostback_Click(object sender, EventArgs e)
        {
            if (RadGrid1.SelectedItems.Count > 0)
            {

            }
        }

        protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            if (!e.IsFromDetailTable)
            {
                RadGrid1.DataSource =
                    GetDataTable(
                        "select TOP 50 *, ContactName from Customers a WHERE a.CustomerID IN(select CustomerID from Orders)");
            }
        }

        protected void RadGrid1_DetailTableDataBind(object source, Telerik.Web.UI.GridDetailTableDataBindEventArgs e)
        {
            GridDataItem dataItem = (GridDataItem)e.DetailTableView.ParentItem;
            switch (e.DetailTableView.Name)
            {
                case "Orders":
                    {
                        string CustomerID = dataItem.GetDataKeyValue("CustomerID").ToString();
                        e.DetailTableView.DataSource = GetDataTable("SELECT TOP 50 * FROM Orders WHERE CustomerID = @customerID", "customerID", CustomerID);
                        break;
                    }

                case "OrderDetails":
                    {
                        string OrderID = dataItem.GetDataKeyValue("OrderID").ToString();
                        e.DetailTableView.DataSource = GetDataTable("SELECT TOP 50 * FROM [Order Details] WHERE OrderID = @OrderID", "OrderID", OrderID);
                        break;
                    }
            }
        }

        public DataTable GetDataTable(string query, string selectParameter = "", string parameterValue = "")
        {
            String ConnString = ConfigurationManager.ConnectionStrings["MainConnectionString"].ConnectionString;
            SqlConnection conn = new SqlConnection(ConnString);
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand(query, conn);

            if (!string.IsNullOrEmpty(selectParameter))
            {
                command.Parameters.Add(new SqlParameter(selectParameter, parameterValue));
            }

            adapter.SelectCommand = command;

            DataTable myDataTable = new DataTable();

            conn.Open();
            try
            {
                adapter.Fill(myDataTable);
            }
            finally
            {
                conn.Close();
            }

            return myDataTable;
        }


        protected void RadGrid1_PreRender(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //RadGrid1.MasterTableView.Items[0].Expanded = true;
                //RadGrid1.MasterTableView.Items[0].ChildItem.NestedTableViews[0].Items[0].Expanded = true;
            }
        }
    }
}