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.OleDb;

public partial class _Default : System.Web.UI.Page
{
    private static DataTable GetDataTable(string query)
    {
        OleDbConnection connection1 = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + System.Web.HttpContext.Current.Server.MapPath("~/App_Data/db1.mdb"));
        OleDbDataAdapter adapter1 = new OleDbDataAdapter();
        adapter1.SelectCommand = new OleDbCommand(query, connection1);
        DataTable table1 = new DataTable();
        connection1.Open();
        try
        {
            adapter1.Fill(table1);
        }
        finally
        {
            connection1.Close();
        }
        return table1;
    }

    protected void SelfRefGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
        (source as RadGrid).DataSource = GetDataTable("SELECT * FROM SelfReferencing");
    }

    protected void SelfRefGrid_ColumnCreated(object sender, GridColumnCreatedEventArgs e)
    {
        if (e.Column.IsBoundToFieldName("ID") || e.Column.IsBoundToFieldName("ParentID"))
        {
            e.Column.Visible = false;
        }
    }


    protected void SelfRefGrid_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridHeaderItem)
        {
            e.Item.Style["display"] = "none";
        }
    }
    protected void RadGrid1_DetailTableDataBind(object source, GridDetailTableDataBindEventArgs e)
    {
        GridDataItem parentItem = e.DetailTableView.ParentItem as GridDataItem;
        e.DetailTableView.DataSource = new object[]{ "" };
        e.DetailTableView.PreRender += new EventHandler(DetailTableView_PreRender);
    }

    void DetailTableView_PreRender(object sender, EventArgs e)
    {
        GridDataItem parentItem = (sender as GridTableView).ParentItem as GridDataItem;
        RadGrid selfRefGrid = (sender as GridTableView).Items[0].FindControl("SelfRefGrid") as RadGrid;
       selfRefGrid.MasterTableView.FilterExpression = "ParentID = " + (int)parentItem.OwnerTableView.DataKeyValues[parentItem.ItemIndex]["EmployeeID"];
       selfRefGrid.Rebind();
    }

}
