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.Data.SqlClient;

public partial class Default : System.Web.UI.Page
{
    protected void OnItemDataBoundHandler(object sender, GridItemEventArgs e)
    {
        if (e.Item.IsInEditMode)
        {
            GridEditableItem item = (GridEditableItem)e.Item;
            if (!(e.Item is IGridInsertItem))
            {
                GridDataItem parentItem =e.Item as GridDataItem;
                string id = parentItem.GetDataKeyValue("CategoryID").ToString();
                RadComboBox categoryCombo = e.Item.FindControl("RadComboBox1") as RadComboBox;
                categoryCombo.DataSource = GetDataTable("SELECT CategoryID, CategoryName FROM Categories");
                categoryCombo.DataBind();
                categoryCombo.SelectedValue = id;

               
                RadComboBox productCombo = parentItem.FindControl("RadComboBox2") as RadComboBox;
                productCombo.DataSource = GetDataTable("SELECT ProductName, ProductID FROM Products WHERE CategoryID='" + id + "'");
                productCombo.DataBind();
                productCombo.SelectedValue = parentItem.GetDataKeyValue("ProductID").ToString();
            }
        }
    }

    protected void OnSelectedIndexChangedHandler(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
    {
        RadComboBox categoryCombo = (RadComboBox)sender;
        string id = categoryCombo.SelectedValue;
        GridDataItem parentItem = (GridDataItem)categoryCombo.NamingContainer;
        RadComboBox productCombo = parentItem.FindControl("RadComboBox2") as RadComboBox;
        productCombo.DataSource = GetDataTable("SELECT ProductName, ProductID FROM Products WHERE CategoryID='" + id + "'");
        productCombo.DataBind();
        productCombo.SelectedValue = parentItem.GetDataKeyValue("ProductID").ToString();

    }

    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;
    }
}
