using System;
using System.Data;
using System.Configuration;
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.Web.UI;

public partial class _Default : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        
    }

    protected void Page_Init(object sender, EventArgs e)
    {
        RadGrid grid = new RadGrid();

        grid.ID = "RadGrid1";
        grid.MasterTableView.DataSourceID = "SqlDataSource1";
        grid.MasterTableView.DataKeyNames = new string[] { "ProductID" };
        grid.Skin = "Default";
        grid.PageSize = 10;
        grid.AllowPaging = true;
        grid.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric;
        grid.AutoGenerateColumns = false;
        grid.AllowFilteringByColumn = true;



        GridBoundColumn boundColumn = new GridBoundColumn();
        grid.MasterTableView.Columns.Add(boundColumn);
        boundColumn.DataField = "ProductID";
        boundColumn.HeaderText = "Product ID";
        boundColumn.UniqueName = "ProductID";


        NewTemplateColumn templateColumn = new NewTemplateColumn();
        templateColumn.ItemTemplate = new TemplateColumn();
        grid.MasterTableView.Columns.Add(templateColumn);
        templateColumn.DataField = "ProductName";
        templateColumn.HeaderText = "ProductName";
        templateColumn.UniqueName = "ProductName";

        ajaxPanel.Controls.Add(grid);

        grid.DataBind();

    }



}

public partial class NewTemplateColumn : GridTemplateColumn
{
    private SqlDataSource comboDataSource = new SqlDataSource(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString(), "select PRODUCTNAME FROM PRODUCTS");
    //RadGrid will call this method when it initializes the controls inside the filtering item cells
    protected override void SetupFilterControls(TableCell cell)
    {
        base.SetupFilterControls(cell);
        cell.Controls.RemoveAt(0);
        RadComboBox combo = new RadComboBox();
        combo.ID = ("RadComboBox1" + this.UniqueName);
        combo.ShowToggleImage = false;
        combo.Skin = "Office2007";
        combo.EnableLoadOnDemand = true;
        combo.AutoPostBack = true;
        combo.MarkFirstMatch = true;
        combo.Height = Unit.Pixel(100);
        combo.ItemsRequested += this.list_ItemsRequested;
        combo.SelectedIndexChanged += this.list_SelectedIndexChanged;
        cell.Controls.AddAt(0, combo);
        cell.Controls.RemoveAt(1);
    }

    //RadGrid will cal this method when the value should be set to the filtering input control(s)
    protected override void SetCurrentFilterValueToControl(TableCell cell)
    {
        base.SetCurrentFilterValueToControl(cell);
        RadComboBox combo = (RadComboBox)cell.Controls[0];
        if ((this.CurrentFilterValue != string.Empty))
        {
            combo.Text = this.CurrentFilterValue;
        }
    }

    //RadGrid will cal this method when the filtering value should be extracted from the filtering input control(s)
    protected override string GetCurrentFilterValueFromControl(TableCell cell)
    {
        RadComboBox combo = (RadComboBox)cell.Controls[0];
        return combo.Text;
    }
    private void list_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
    {
        ((RadComboBox)o).DataValueField = "ProductName";
        ((RadComboBox)o).DataTextField = "ProductName";
        comboDataSource.SelectCommand = "SELECT DISTINCT ProductName FROM Products WHERE ProductName LIKE '" + e.Text + "%'";
        ((RadComboBox)o).DataSource = comboDataSource.Select(new DataSourceSelectArguments());
        ((RadComboBox)o).DataBind();
    }

    private void list_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
    {
        GridFilteringItem filterItem = (GridFilteringItem)((RadComboBox)o).NamingContainer;
        if ((this.UniqueName == "ProductName"))
        {
            //this is filtering for integer column type 
            filterItem.FireCommandEvent("Filter", new Pair("EqualTo", this.UniqueName));
        }
        //filtering for string column type
        filterItem.FireCommandEvent("Filter", new Pair("Contains", this.UniqueName));
    }

}

public partial class TemplateColumn : ITemplate
{

    public void InstantiateIn(Control container)
    {

        LiteralControl literalControl = new LiteralControl();
        literalControl.ID = "literalControl";
        literalControl.DataBinding += new EventHandler(literalControl_DataBinding);

        CheckBox box = new CheckBox();

        box.ID = "boxControl";

        box.DataBinding += new EventHandler(box_DataBinding);

        container.Controls.Add(literalControl);
        container.Controls.Add(box);

    }

    void box_DataBinding(object sender, EventArgs e)
    {
        CheckBox box = (CheckBox)sender;

        GridDataItem container = box.NamingContainer as GridDataItem;

        if (container != null)
            box.Checked = (bool)((DataRowView)container.DataItem)["Discontinued"];
        else
        {
            GridEditFormItem editFormContainer = box.NamingContainer as GridEditFormItem;
            box.Enabled = (bool)((DataRowView)editFormContainer.DataItem)["Discontinued"];

        }
    }

    protected void literalControl_DataBinding(object sender, EventArgs e)
    {
        LiteralControl l = (LiteralControl)sender;

        GridDataItem container = l.NamingContainer as GridDataItem;

        if (container != null)
        {
            l.Text = ((DataRowView)container.DataItem)["ProductName"].ToString();

        }
        else
        {
            GridEditFormItem editFormContainer = l.NamingContainer as GridEditFormItem;
            l.Text = ((DataRowView)editFormContainer.DataItem)["ProductName"].ToString();
        }
    }




}
