using System;
using System.Data;
using System.Web.UI;
using Telerik.Web.UI;

public partial class Default3 : Page
{
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            GridDropDownListColumnEditor categoryColumnEditor = ((GridEditableItem)e.Item).EditManager.GetColumnEditor("CategoryID") as GridDropDownListColumnEditor;

            RadComboBox comboBox = categoryColumnEditor.ComboBoxControl;
            comboBox.AutoPostBack = true;
            comboBox.SelectedIndexChanged += Default3_SelectedIndexChanged;
        }
    }

    void Default3_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
    {
        GridEditableItem item = ((Control) sender).NamingContainer as GridEditableItem;

        int categoryId = int.Parse(e.Value);
        RadComboBox comboBox = item["ProductID"].Controls[0] as RadComboBox;
        comboBox.DataSource = GetProductsByCategory(categoryId);
        comboBox.DataBind();
    }

    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem && !e.Item.IsInEditMode)
        {
            //add code to populate the label controls
        }
        else if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            GridDropDownListColumnEditor categoryColumnEditor = ((GridEditableItem)e.Item).EditManager.GetColumnEditor("CategoryID") as GridDropDownListColumnEditor;

            RadComboBox categories = categoryColumnEditor.ComboBoxControl;
            categoryColumnEditor.DataSource = GetCategories();
            categoryColumnEditor.DataBind();

            int? categoryId = null;
            if (categories.Items.Count > 0)
            {
                categoryId = (int) DataBinder.Eval(e.Item.DataItem, "CategoryID");
                categories.FindItemByValue(categoryId.ToString()).Selected = true;
            }

            if (categoryId != null)
            {
                GridDropDownListColumnEditor productColumnEditor = ((GridEditableItem)e.Item).EditManager.GetColumnEditor("ProductID") as GridDropDownListColumnEditor;

                RadComboBox productsCombo = productColumnEditor.ComboBoxControl;
                productColumnEditor.DataSource = GetProductsByCategory(categoryId.GetValueOrDefault());
                productColumnEditor.DataBind();
                if (productsCombo.Items.Count > 0)
                {
                    productsCombo.FindItemByValue(DataBinder.Eval(e.Item.DataItem, "ProductID").ToString()).Selected = true;
                }
            }
        }
    }

    private DataTable GetProductsByCategory(int categoryId)
    {
        SqlDataSource3.SelectParameters["CategoryID"].DefaultValue = categoryId.ToString();
        return ((DataView)SqlDataSource3.Select(new DataSourceSelectArguments())).Table;
    }

    protected DataTable GetCategories()
    {
        return ((DataView)SqlDataSource2.Select(new DataSourceSelectArguments())).Table;
    }
}
