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

public partial class Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        destinationCodesGrid.ItemCommand += new GridCommandEventHandler(destinationCodesGrid_ItemCommand);
    }

    void destinationCodesGrid_ItemCommand(object source, GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.ExpandCollapseCommandName)
        {
            GridDataItem item = e.Item as GridDataItem;
            if (!item.Expanded)
            {
                GridNestedViewItem nestedItem = (GridNestedViewItem)item.ChildItem;
                string dataKeyValue = Convert.ToString(((GridDataItem)(nestedItem.ParentItem)).GetDataKeyValue("DialID"));
                RadGrid tempGrid = (RadGrid)nestedItem.FindControl("detailsGrid");
                tempGrid.Rebind();

            }
        }
    }
    protected void destinationCodesGrid_OnNeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
        DataClasses1DataContext context = new DataClasses1DataContext();
        destinationCodesGrid.DataSource = from d in context.Dials
                                           select d;
    }


    protected void detailsGrid_OnNeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
        DataClasses1DataContext context = new DataClasses1DataContext();
        RadGrid grid = (RadGrid)source;
        GridNestedViewItem nestedItem = (GridNestedViewItem)grid.NamingContainer;
        string dataKeyValue = Convert.ToString(((GridDataItem)(nestedItem.ParentItem)).GetDataKeyValue("DialID"));
        RadGrid tempGrid = (RadGrid)nestedItem.FindControl("detailsGrid");
        int dialId = Convert.ToInt32(dataKeyValue);
        tempGrid.DataSource = (from dt in context.DetailTables
                              where dt.DialID == dialId
                              select dt).ToList();
    }
}
