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;
using System.Collections;

public partial class Default : System.Web.UI.Page
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        RadGrid1.UpdateCommand += new GridCommandEventHandler(RadGrid1_UpdateCommand);
    }

    void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
    {
        GridEditableItem editedItem = e.Item as GridEditableItem;
        RadComboBox categoryCombo = e.Item.FindControl("RadComboBox1") as RadComboBox;
        string category = categoryCombo.SelectedValue;
        string categoryName = categoryCombo.SelectedItem.Text;
        RadComboBox productCombo = e.Item.FindControl("RadComboBox2") as RadComboBox;
        string product = productCombo.SelectedValue;
        string productName = productCombo.SelectedItem.Text;

        ProductsDataSource.UpdateParameters["ProductID"].DefaultValue = product;
        ProductsDataSource.UpdateParameters["ProductName"].DefaultValue = productName;
        ProductsDataSource.UpdateParameters["CategoryID"].DefaultValue = category;
    }

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