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

public partial class BulledtedList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        RadGrid1.NeedDataSource += new Telerik.Web.UI.GridNeedDataSourceEventHandler(RadGrid1_NeedDataSource);
    }

    void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        if (!e.IsFromDetailTable)
        {
            RadGrid1.DataSource = GetDataTable("SELECT * FROM Customers");
        }
    }

    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;
    }
    protected void RadGrid1_DetailTableDataBind(object sender, 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 * FROM Orders WHERE CustomerID = '" + CustomerID + "'");
                    break;
                }
        }
    }

    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem && e.Item.OwnerTableView.Name == "Orders")
        {

            GridDataItem dataItem = e.Item as GridDataItem;
            GridDataItem parentItem = dataItem.OwnerTableView.ParentItem;
            string customerID = parentItem.GetDataKeyValue("CustomerID").ToString();
            BulletedList bulletedList = dataItem["TemplateColumn1"].Controls[1] as BulletedList;
            bulletedList.DataTextField = "ContactName";
            bulletedList.DataValueField = "City";
            bulletedList.DataSource = GetDataTable("SELECT ContactName, City FROM Customers WHERE CustomerID LIKE '" + customerID[0] + "%'");
            bulletedList.DataBind();
            
        }
    }
}