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.Web.UI;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Init(object sender, System.EventArgs e)
    {
        DefineGridStructure();
    }
    private void DefineGridStructure()
    {
        RadGrid grid = new RadGrid();
        grid.ID = "RadGrid1";
        grid.Skin = "Vista";
        grid.Width = Unit.Percentage(100);
        grid.PageSize = 5;
        grid.AllowPaging = true;
        grid.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric;
        grid.AutoGenerateColumns = false;
        grid.MasterTableView.HierarchyLoadMode = GridChildLoadMode.ServerBind;
        grid.NeedDataSource += new GridNeedDataSourceEventHandler(grid_NeedDataSource);
        grid.DetailTableDataBind += new GridDetailTableDataBindEventHandler(grid_DetailTableDataBind);
        grid.PreRender += new EventHandler(grid_PreRender);

        //Add Customers table
        grid.MasterTableView.PageSize = 15;
        grid.MasterTableView.Width = Unit.Percentage(100);
        grid.MasterTableView.DataKeyNames = new string[] { "CustomerID" };
        GridBoundColumn boundColumn = new GridBoundColumn();
        boundColumn.DataField = "CustomerID";
        boundColumn.HeaderText = "Customer ID";
        grid.MasterTableView.Columns.Add(boundColumn);
        boundColumn = new GridBoundColumn();
        boundColumn.DataField = "ContactName";
        boundColumn.HeaderText = "Contact Name";
        grid.MasterTableView.Columns.Add(boundColumn);
        //Add Orders table
        GridTableView tableViewOrders = new GridTableView(grid);
        tableViewOrders.Width = Unit.Percentage(100);        
        boundColumn = new GridBoundColumn();
        boundColumn.DataField = "OrderID";
        boundColumn.HeaderText = "OrderID";
        tableViewOrders.Columns.Add(boundColumn);
        boundColumn = new GridBoundColumn();
        boundColumn.DataField = "OrderDate";
        boundColumn.HeaderText = "Date Ordered";
        tableViewOrders.Columns.Add(boundColumn);
        grid.MasterTableView.DetailTables.Add(tableViewOrders);

        // Add the grid to the placeholder
        this.PlaceHolder1.Controls.Add(grid);
    }

    protected void grid_DetailTableDataBind(object source, GridDetailTableDataBindEventArgs e)
    {
        GridDataItem dataItem = (GridDataItem)e.DetailTableView.ParentItem;
        string CustomerID = dataItem.GetDataKeyValue("CustomerID").ToString();
        e.DetailTableView.DataSource = GetDataTable("SELECT * FROM Orders WHERE CustomerID = '" + CustomerID + "'");
    }

    protected void grid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
        if (!e.IsFromDetailTable)
        {
            (source as RadGrid).DataSource = GetDataTable("SELECT * FROM Customers");
        }
    } 
    protected void grid_PreRender(object sender, EventArgs e)
    {
        HideExpandColumnRecursive((sender as RadGrid).MasterTableView);
    }
    public void HideExpandColumnRecursive(GridTableView tableView)
    {
        GridItem[] nestedViewItems = tableView.GetItems(GridItemType.NestedView);
        foreach (GridNestedViewItem nestedViewItem in nestedViewItems)
        {
            foreach (GridTableView nestedView in nestedViewItem.NestedTableViews)
            {
                if (nestedView.Items.Count == 0)
                {
                    TableCell cell = nestedView.ParentItem["ExpandColumn"];
                    cell.Controls[0].Visible = false;
                    nestedViewItem.Visible = false;
                }
                if (nestedView.HasDetailTables)
                {
                    HideExpandColumnRecursive(nestedView);
                }
            }
        }
    }
    public DataTable GetDataTable(string query)
    {
        String ConnString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
        SqlConnection conn = new SqlConnection(ConnString);
        SqlDataAdapter adapter = new SqlDataAdapter();
        adapter.SelectCommand = new SqlCommand(query, conn);

        DataTable myDataTable = new DataTable();

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

        return myDataTable;
    }

}
