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.WebControls;
using System.Data.OleDb;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    override protected void OnInit(EventArgs e)
		{
			InitializeComponent();
			base.OnInit(e);
		}

    private void InitializeComponent()
    {
        this.RadGrid1.NeedDataSource += new Telerik.WebControls.GridNeedDataSourceEventHandler(this.RadGrid1_NeedDataSource);
        this.RadGrid1.UpdateCommand += new Telerik.WebControls.GridCommandEventHandler(this.RadGrid1_UpdateCommand);
        this.RadGrid1.CreateColumnEditor += new Telerik.WebControls.GridCreateColumnEditorEventHandler(this.RadGrid1_CreateColumnEditor);
        this.Load += new System.EventHandler(this.Page_Load);
    }

    private void RadGrid1_CreateColumnEditor(object sender, Telerik.WebControls.GridCreateColumnEditorEventArgs e)
    {
        if (e.Column.UniqueName == "ComboColumn")
        {
            ComboEditor comboEditor = new ComboEditor();
            e.ColumnEditor = comboEditor;
        }
    }

    private void RadGrid1_NeedDataSource(object source, Telerik.WebControls.GridNeedDataSourceEventArgs e)
    {
        if (Session["DataSource"] == null)
        {
            OleDbConnection MyOleDbConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("~/App_Data/Nwind.mdb"));
            OleDbDataAdapter MyOleDbDataAdapter = new OleDbDataAdapter();
            MyOleDbDataAdapter.SelectCommand = new OleDbCommand("SELECT TOP 5 CustomerID, ContactName, Address FROM Customers", MyOleDbConnection);
            DataTable myDataTable = new DataTable();
            MyOleDbConnection.Open();
            try
            {
                MyOleDbDataAdapter.Fill(myDataTable);
            }
            finally
            {
                MyOleDbConnection.Close();
            }
            RadGrid1.DataSource = myDataTable.DefaultView;
            Session["DataSource"] = myDataTable;
        }
        else
        {
            RadGrid1.DataSource = Session["DataSource"];
        }

    }

    private void RadGrid1_UpdateCommand(object source, Telerik.WebControls.GridCommandEventArgs e)
    {
        GridEditableItem editedItem = (e.Item as GridEditableItem);
        GridEditManager editMan = editedItem.EditManager;

        foreach (GridColumn column in RadGrid1.MasterTableView.RenderColumns)
        {
            if (column is IGridEditableColumn)
            {
                IGridEditableColumn editableCol = column as IGridEditableColumn;
                if (editableCol.IsEditable)
                {
                    IGridColumnEditor editor = editMan.GetColumnEditor(editableCol);
                    string editorType = editor.ToString();

                    object editorValue = null;

                    if (editor is GridTextColumnEditor)
                    {
                        editorValue = (editor as GridTextColumnEditor).Text;
                    }
                    if (editor is ComboEditor)
                    {
                        TableCell cell = (editor as ComboEditor).ContainerControl as TableCell;
                        editorValue = (cell.FindControl("RadComboBox1") as RadComboBox).Text;
                    }
                    try
                    {
                        DataTable sourceTable = Session["DataSource"] as DataTable;
                        DataRow[] changedRows = sourceTable.Select("CustomerID = '" + editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["CustomerID"] + "'");

                        if (column.UniqueName == "ComboColumn")
                        {
                            changedRows[0]["ContactName"] = editorValue;
                        }
                        else
                        {
                            changedRows[0][column.UniqueName] = editorValue;
                        }

                        sourceTable.AcceptChanges();
                    }
                    catch (Exception ex)
                    {
                        Response.Write(ex.StackTrace);
                    }
                }
            }
        }

    }
}

public class ComboEditor : GridDropDownColumnEditor
{
    private RadComboBox combo;

    public RadComboBox ComboControl
    {
        get
        {
            this.EnsureControlsCreated();
            return this.combo;
        }
    }

    protected override void CreateControls()
    {
        this.combo = new RadComboBox();
        this.combo.EnableLoadOnDemand = true;
        this.combo.ExternalCallBackPage = "ComboStreamer.aspx";
        this.combo.ShowToggleImage = true;
        this.combo.NoWrap = false;
        this.combo.MarkFirstMatch = true;
        this.combo.AllowCustomText = false;
        this.combo.Width = Unit.Pixel(150);
    }

    protected override void AddControlsToContainer()
    {
        this.EnsureControlsCreated();

        this.ContainerControl.Controls.Add(this.combo);
        this.combo.ID = "RadComboBox1";
        this.combo.DataMember = this.DataMember;
        this.combo.DataTextField = this.DataTextField;
        this.combo.DataValueField = this.DataValueField;
        this.combo.DataSource = this.DataSource;
        this.combo.DataBind();
    }

    public void OnComboBind(object sender, EventArgs args)
    {
    }

    public override int SelectedIndex
    {
        get
        {
            return this.combo.SelectedIndex;
        }
        set
        {
            int i = 0;
            foreach (RadComboBoxItem item in this.combo.Items)
            {
                if (i == value)
                {
                    item.Selected = true;
                    break;
                }

                i++;
            }
        }
    }

    public override string SelectedValue
    {
        get
        {
            return (SelectedIndex >= 0) ? this.combo.SelectedItem.Value : "";
        }
        set
        {
            foreach (RadComboBoxItem item in this.combo.Items)
            {
                if (item.Value == value)
                {
                    item.Selected = true;
                    break;
                }
            }
        }
    }

    public override string SelectedText
    {
        get
        {
            return (SelectedIndex >= 0) ? this.combo.SelectedItem.Text : "";
        }
        set
        {
            foreach (RadComboBoxItem item in this.combo.Items)
            {
                if (item.Text == value)
                {
                    item.Selected = true;
                    break;
                }
            }
        }
    }

    public override void DataBind()
    {
        this.combo.DataBind();
    }

    protected override void LoadControlsFromContainer()
    {
        this.combo = (RadComboBox)this.ContainerControl.Controls[0];
    }
}
