using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Data;
using System.Collections.Specialized;
using System.Web.UI.HtmlControls;

public partial class Default16 : System.Web.UI.Page
{
    RadGrid radGrid;
    const string Skin = "Vista";
    override protected void OnInit(EventArgs e)
    {
        radGrid = new RadGrid();

        //radGrid.EditCommand += new GridCommandEventHandler(radGrid_EditCommand);
        //radGrid.UpdateCommand += new GridCommandEventHandler(radGrid_UpdateCommand);
        //radGrid.CancelCommand += new GridCommandEventHandler(radGrid_CancelCommand);
        radGrid.NeedDataSource += new GridNeedDataSourceEventHandler(radGrid_NeedDataSource);
        //radGrid.ItemDataBound += new GridItemEventHandler(radGrid_ItemDataBound);
        radGrid.ItemCommand += new GridCommandEventHandler(radGrid_ItemCommand);

        populatePropertyOfGrid();
    }

    void radGrid_ItemCommand(object source, GridCommandEventArgs e)
    {
        Label1.Text = e.CommandName;
    }

    void radGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
        DataTable table = new DataTable();
        table.Columns.Add("PageConfigId");
        table.Columns.Add("IdDetailSearch");
        table.Columns.Add("IdSearchSearch");
        table.Columns.Add("DataflowId");
        table.Columns.Add("IsMandatoryForExport");
        table.Columns.Add("DataElement");
        table.Columns.Add("IsAvailable");
        table.Rows.Add(1000, true, true, 1024, false, "DataElement", true);
        radGrid.DataSource = table;
    }
    public void populatePropertyOfGrid()
    {
        radGrid.ID = "radGrid";
        radGrid.Skin = "Vista";        
        radGrid.Width = Unit.Percentage(100);
        radGrid.AutoGenerateColumns = false;
        radGrid.MasterTableView.EditMode = GridEditMode.InPlace;

        string[] DataKeys = new string[5];
        DataKeys[0] = "PageConfigId";
        DataKeys[1] = "IdDetailSearch";
        DataKeys[2] = "IdSearchSearch";
        DataKeys[3] = "DataflowId";
        DataKeys[4] = "IsMandatoryForExport";

        radGrid.MasterTableView.DataKeyNames = DataKeys;

        GridTemplateColumn gridTemplateEditLink = new GridTemplateColumn();
        gridTemplateEditLink.ItemTemplate = new FeildsGridTemplate(ListItemType.Item, "LinkButton", "linkEdit", Skin, "edit", "Edit", null);
        gridTemplateEditLink.EditItemTemplate = new FeildsGridTemplate(ListItemType.EditItem, "LinkButton", "linkUpdate", Skin, "update", "Update", null);
        gridTemplateEditLink.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
        gridTemplateEditLink.HeaderStyle.VerticalAlign = VerticalAlign.Middle;
        gridTemplateEditLink.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
        gridTemplateEditLink.ItemStyle.VerticalAlign = VerticalAlign.Middle;
        radGrid.MasterTableView.Columns.Add(gridTemplateEditLink);

        GridTemplateColumn gridTemplateDataElement = new GridTemplateColumn();

        gridTemplateDataElement.ItemTemplate = new FeildsGridTemplate(ListItemType.Item, "Label", "lblDataElement", Skin, "DataElement", "", null);
        gridTemplateDataElement.EditItemTemplate = new FeildsGridTemplate(ListItemType.EditItem, "Label", "lblEditDataElement", Skin, "DataElement", "", null);
        gridTemplateDataElement.HeaderText = "Data Element";
        // gridTemplateDataElement.SortExpression = "DataElement";
        gridTemplateDataElement.DataField = "DataElement";
        gridTemplateDataElement.HeaderStyle.Width = Unit.Pixel(120);
        gridTemplateDataElement.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
        gridTemplateDataElement.HeaderStyle.VerticalAlign = VerticalAlign.Middle;

        gridTemplateDataElement.ItemStyle.Width = Unit.Pixel(120);
        gridTemplateDataElement.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
        gridTemplateDataElement.ItemStyle.VerticalAlign = VerticalAlign.Middle;

        radGrid.MasterTableView.Columns.Add(gridTemplateDataElement);



        GridTemplateColumn gridTemplateAvail = new GridTemplateColumn();

        gridTemplateAvail.ItemTemplate = new FeildsGridTemplate(ListItemType.Item, "CheckBox", "chkItemAvailable", Skin, "IsAvailable", "", null);
        gridTemplateAvail.EditItemTemplate = new FeildsGridTemplate(ListItemType.EditItem, "CheckBox", "chkEditAvailable", Skin, "IsAvailable", "", null);

        gridTemplateAvail.HeaderText = "Avail.";
        // gridTemplateAvail.SortExpression = "IsAvailable";
        gridTemplateAvail.DataField = "IsAvailable";
        gridTemplateAvail.HeaderStyle.Width = Unit.Pixel(20);
        gridTemplateAvail.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
        gridTemplateAvail.HeaderStyle.VerticalAlign = VerticalAlign.Middle;

        gridTemplateAvail.ItemStyle.Width = Unit.Pixel(20);
        gridTemplateAvail.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
        gridTemplateAvail.ItemStyle.VerticalAlign = VerticalAlign.Middle;

        radGrid.MasterTableView.Columns.Add(gridTemplateAvail);

        Panel1.Controls.Add(radGrid);
    }
}
public class FeildsGridTemplate : IBindableTemplate    // IBindableTemplate
{
    ListItemType templateType;
    string addedControlType;
    string controlId;
    string commandName;
    string Skin;
    string columnName;

    object ControlInput;

    public System.Collections.Specialized.IOrderedDictionary ExtractValues(System.Web.UI.Control container)
    {
        OrderedDictionary od = new OrderedDictionary();
        // od.Add("ProviderName", ((TextBox)(((GridEditFormItem)(container)).FindControl("MyTextBox"))).Text);
        return od;
    }



    /// <summary>
    /// This Contructor for populate Item,Footer,Edit Property
    /// </summary>
    /// <param name="type"></param>
    /// <param name="pr_ControlType"></param>
    /// <param name="pr_controlId"></param>
    /// <param name="pr_GridId"></param>
    /// <param name="pr_columnName"></param>
    public FeildsGridTemplate(ListItemType type, string pr_ControlType, string pr_controlId, string pr_Skin, string pr_columnName, string pr_commandName, object pr_ControlInput)
    {
        templateType = type;
        addedControlType = pr_ControlType;
        controlId = pr_controlId;
        Skin = pr_Skin;
        columnName = pr_columnName;
        commandName = pr_commandName;
        ControlInput = pr_ControlInput;
    }


    public void InstantiateIn(System.Web.UI.Control container)
    {
        CheckBox chkBox = new CheckBox();
        LinkButton lnkBtn = new LinkButton();
        //CustomizeTextBox txtBox = new CustomizeTextBox();
        TextBox txtBox = new TextBox();
        Label lbl = new Label();
        // DropDownControl dropDownControl = new DropDownControl();
        System.Web.UI.WebControls.DropDownList dropDownControl = new System.Web.UI.WebControls.DropDownList();


        switch (templateType)
        {
            case ListItemType.EditItem:
                #region EditItem
                if (addedControlType == "CheckBox")
                {
                    chkBox.ID = controlId;
                    chkBox.Text = columnName;
                    container.Controls.Add(chkBox);
                    chkBox.DataBinding += new EventHandler(boolValueEdit_DataBinding); // This Event for bind this object

                }
                else if (addedControlType == "TextBox")
                {
                    txtBox.Width = 150;
                    //  txtBox.Connectionstring = Skin; // This is connection string for dataBase connection
                    txtBox.ID = controlId;
                    txtBox.Text = columnName;

                    txtBox.DataBinding += new EventHandler(txtBoxEditDataBinding);
                    container.Controls.Add(txtBox);
                }
                else if (addedControlType == "LinkButton")
                {
                    HtmlTable table = new HtmlTable();
                    table.Attributes.Add("class", "select_bdr");

                    table.CellPadding = 3;
                    table.CellSpacing = 3;

                    HtmlTableRow tr = new HtmlTableRow();
                    HtmlTableCell td1 = new HtmlTableCell();
                    td1.Style.Value = "border-style:none !important;";

                    HtmlTableCell td2 = new HtmlTableCell();
                    td2.Style.Value = "border-style:none !important;";

                    Label td1GapLeft = new Label();
                    Label td1GapRight = new Label();
                    Label td2GapLeft = new Label();
                    Label td2GapRight = new Label();

                    td1GapLeft.Text = "[";
                    td1GapLeft.Style.Value = "font-size: 10px ! important;color:#0000EE;";

                    td1GapRight.Text = "]";
                    td1GapRight.Style.Value = "font-size: 10px ! important;color:#0000EE;";

                    td2GapLeft.Text = "[";
                    td2GapLeft.Style.Value = "font-size: 10px ! important;color:#0000EE;";

                    td2GapRight.Text = "]";
                    td2GapRight.Style.Value = "font-size: 10px ! important;color:#0000EE;";

                    lnkBtn.ID = controlId;
                    lnkBtn.Text = columnName;
                    lnkBtn.Style.Value = "font-size: 10px ! important;color:#0000EE; margin-left:3px; margin-right:3px;";
                    lnkBtn.CommandName = "Update";

                    td1.Controls.Add(td1GapLeft);
                    td1.Controls.Add(lnkBtn);
                    td1.Controls.Add(td1GapRight);
                    tr.Controls.Add(td1);


                    LinkButton linkCancel = new LinkButton();
                    linkCancel.ID = "linkCancel";
                    linkCancel.Text = "cancel";
                    linkCancel.Style.Value = "font-size: 10px ! important;color:#0000EE; margin-left:3px; margin-right:3px;";
                    linkCancel.CommandName = "Cancel";

                    td2.Controls.Add(td2GapLeft);
                    td2.Controls.Add(linkCancel);
                    td2.Controls.Add(td2GapRight);
                    tr.Controls.Add(td2);



                    table.Controls.Add(tr);
                    container.Controls.Add(table);
                }
                else if (addedControlType == "Label")
                {
                    lbl.ID = controlId;
                    lbl.Text = columnName;
                    lbl.DataBinding += new EventHandler(lblEdit_DataBinding);
                    container.Controls.Add(lbl);
                }
                else if (addedControlType == "Dropdown")
                {
                    dropDownControl.ID = controlId;
                    dropDownControl.SkinID = columnName;
                    //DropdownPropertyClass dropdownPropertyClass = (DropdownPropertyClass)ControlInput;
                    if (Skin != "")
                    {
                        //  dropDownControl.DropDownListSkin = dropdownPropertyClass.Skin;
                        //  dropDownControl.EnableEmbeddedSkins = dropdownPropertyClass.EnableEmbeddedSkins;
                    }

                    //  dropDownControl.ConnectionString = dropdownPropertyClass.ConnectionString;

                    //  dropDownControl.MainTableCssClass = "select_bdr";

                    //dropDownControl.DataTextField = dropdownPropertyClass.DataTextField;
                    //dropDownControl.DataValueField = dropdownPropertyClass.DataValueField;

                    //  if (dropdownPropertyClass.DataSource != null)
                    //  {
                    //dropDownControl.DataSource = dropdownPropertyClass.DataSource;
                    // dropDownControl.DataBind();

                    //  }


                    //   dropDownControl.DataBinding += new EventHandler(dropDownControlEdit_DataBinding);
                    container.Controls.Add(dropDownControl);
                }
                break;
                #endregion
            case ListItemType.Item:
                #region Item
                if (addedControlType == "CheckBox")
                {
                    chkBox.ID = controlId;
                    chkBox.Enabled = false;
                    chkBox.Text = columnName;
                    container.Controls.Add(chkBox);
                    chkBox.DataBinding += new EventHandler(boolValue_DataBinding); // This Event for bind this object

                }
                else if (addedControlType == "LinkButton")
                {
                    HtmlTable table = new HtmlTable();
                    table.Attributes.Add("class", "select_bdr");

                    table.CellPadding = 0;
                    table.CellSpacing = 0;

                    HtmlTableRow tr = new HtmlTableRow();
                    HtmlTableCell td1 = new HtmlTableCell();
                    td1.Style.Value = "border-style:none !important;";
                    Label td1GapLeft = new Label();
                    Label td1GapRight = new Label();


                    td1GapLeft.Text = "[";
                    td1GapLeft.Style.Value = "font-size: 10px ! important;color:#0000EE;";

                    td1GapRight.Text = "]";
                    td1GapRight.Style.Value = "font-size: 10px ! important;color:#0000EE;";


                    lnkBtn.ID = controlId;
                    lnkBtn.Text = columnName;
                    lnkBtn.Style.Value = "font-size: 10px ! important;color:#0000EE; margin-left:3px; margin-right:3px;";
                    lnkBtn.CommandName = "Edit";

                    td1.Controls.Add(td1GapLeft);
                    td1.Controls.Add(lnkBtn);
                    td1.Controls.Add(td1GapRight);
                    tr.Controls.Add(td1);



                    table.Controls.Add(tr);
                    container.Controls.Add(table);

                }
                else if (addedControlType == "Label")
                {
                    lbl.ID = controlId;
                    lbl.Text = columnName;
                    lbl.DataBinding += new EventHandler(lbl_DataBinding);
                    container.Controls.Add(lbl);
                }

                #endregion
                break;
        }

    }


    /// <summary>
    /// This Event for populating Label Text Template Item
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void lbl_DataBinding(object sender, EventArgs e)
    {
        Label lbl = (Label)sender;
        string id = lbl.ID;
        GridDataItem container = (GridDataItem)lbl.NamingContainer;
        if (lbl != null && lbl.Text != "")
        {
            try  // User Don't want to Bind this object, then dataColumn is not found in dataSource then runtime exception occur
            {
                lbl.Text = Convert.ToString(((DataRowView)container.DataItem)[lbl.Text]);
            }
            catch (Exception ex)
            {

            }
        }
    }


    /// <summary>
    /// This checkbox event of item template
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void boolValue_DataBinding(object sender, EventArgs e)
    {
        CheckBox cBox = (CheckBox)sender;
        GridDataItem container = (GridDataItem)cBox.NamingContainer;
        if (cBox.Text != "")
        {
            try  // User Don't want to Bind this object, then dataColumn is not found in dataSource then runtime exception occur
            {
                cBox.Checked = (bool)((DataRowView)container.DataItem)[cBox.Text];
            }
            catch (Exception ex)
            {

            }
            cBox.Text = "";
        }

        // cBox.Text = "";
    }

    /// <summary>
    /// This Event to Populate Template Edit Dropdown
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void dropDownControlEdit_DataBinding(object sender, EventArgs e)
    {
        RadComboBox dropDownControl = (RadComboBox)sender;
        GridEditableItem container = (GridEditableItem)dropDownControl.NamingContainer;
        if (dropDownControl != null && dropDownControl.SkinID != "")
        {
            try
            {
                dropDownControl.SelectedItem.Text = Convert.ToString(((DataRowView)container.DataItem)[dropDownControl.SkinID]);
            }
            catch (Exception ex)
            {

            }
        }
    }

    /// <summary>
    /// This checkbox event of Edit template
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void boolValueEdit_DataBinding(object sender, EventArgs e)
    {
        CheckBox cBox = (CheckBox)sender;
        GridEditableItem container = (GridEditableItem)cBox.NamingContainer;
        if (cBox.Text != "")
        {
            try  // User Don't want to Bind this object, then dataColumn is not found in dataSource then runtime exception occur
            {
                cBox.Checked = (bool)((DataRowView)container.DataItem)[cBox.Text];
            }
            catch (Exception ex)
            {

            }
            cBox.Text = "";
        }

        // cBox.Text = "";
    }


    /// <summary>
    /// This Event for populating Label Text Template Edit
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void lblEdit_DataBinding(object sender, EventArgs e)
    {
        Label lbl = (Label)sender;
        GridEditableItem container = (GridEditableItem)lbl.NamingContainer;
        if (lbl != null && lbl.Text != "")
        {
            try  // User Don't want to Bind this object, then dataColumn is not found in dataSource then runtime exception occur
            {
                lbl.Text = Convert.ToString(((DataRowView)container.DataItem)[lbl.Text]);
            }
            catch (Exception ex)
            {

            }
        }
    }


    /// <summary>
    /// This event For Binding edit TextBox
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void txtBoxEditDataBinding(object sender, EventArgs e)
    {
        //CustomizeTextBox txtBoxItem = (CustomizeTextBox)sender;
        TextBox txtBoxItem = (TextBox)sender;
        GridEditableItem container = (GridEditableItem)txtBoxItem.NamingContainer;
        if (txtBoxItem != null && txtBoxItem.Text != "")
        {
            try  // User Don't want to Bind this object, then dataColumn is not found in dataSource then runtime exception occur
            {
                txtBoxItem.Text = Convert.ToString(((DataRowView)container.DataItem)[txtBoxItem.Text]);
            }
            catch (Exception ex)
            {

            }
        }

    }

}

 [Serializable()]
 public class DropdownDataSource
 {
     public string PageConfigId = "";
     public DataTable DataSource = null;

 }