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

public partial class TestTinyInt : System.Web.UI.Page
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        RadGrid1.PreRender += new EventHandler(RadGrid1_PreRender);
    }

    public List<String> CustomSelectedIndexes
    {
        get
        {
            return (List<String>)ViewState["CustomSelectedIndexes"] ?? new List<String>();
        }
        set 
        {
            ViewState["CustomSelectedIndexes"] = value;
        }
    }

    protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
        //RadGrid1.PagerStyle.PagerTextFormat = "Change page: {4}    Displaying page {0} of {1}, items {2} to {3} of {5} ";
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        //if (!IsPostBack)
        //{
        //    RadGrid1.PagerStyle.PagerTextFormat = "Change page: {4}    Displaying page {0} of {1}, items {2} to {3} of {5} ";
        //}

        if (!IsPostBack)
        {
            for (int i = 0; i < RadGrid1.PageSize; i++)
            {
                //RadGrid1.EditIndexes.Add(i);
            }
        }

    }

    protected void btnEdit_Click(object sender, EventArgs e)
    {
        foreach (var item in RadGrid1.SelectedItems)
        {
            (item as GridDataItem).FireCommandEvent("Edit", null);
        }
    }
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridGroupHeaderItem)
        {
            var item = e.Item as GridGroupHeaderItem;

            if (item.HasChildItems)
            {
                GridItem[] childItems = item.GetChildItems();
                item.DataCell.Text += String.Format(" Count:{0}", childItems.Count());
            }
        }
    }
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            GridEditableItem item = e.Item as GridEditableItem;
            GridTextBoxColumnEditor editor = (GridTextBoxColumnEditor)item.EditManager.GetColumnEditor("TinyIntValue");
            TableCell cell = (TableCell)editor.TextBoxControl.Parent;

            GridTextBoxColumnEditor editor2 = (GridTextBoxColumnEditor)item.EditManager.GetColumnEditor("TinyIntValue2");

            RequiredFieldValidator rfvTinyIntValue = new RequiredFieldValidator();
            rfvTinyIntValue.ID = "rfvTinyIntValue";
            editor.TextBoxControl.ID = "TinyIntValueTextBox";
            rfvTinyIntValue.ControlToValidate = editor.TextBoxControl.ID;
            rfvTinyIntValue.Display = ValidatorDisplay.Dynamic;
            rfvTinyIntValue.ErrorMessage = "The value is required!";
            cell.Controls.Add(rfvTinyIntValue);

            GridDateTimeColumnEditor editorDataFinal = (GridDateTimeColumnEditor)item.EditManager.GetColumnEditor("GridDateTimeColumn1");
            TableCell cellDataFinal = (TableCell)editorDataFinal.PickerControl.Parent;

            RequiredFieldValidator rfvTestDate = new RequiredFieldValidator();
            rfvTestDate.ID = "rfvTestDate";
            rfvTestDate.ControlToValidate = editorDataFinal.PickerControl.ID;
            rfvTestDate.Display = ValidatorDisplay.Dynamic;
            rfvTestDate.ErrorMessage = "The value is required!";
            cellDataFinal.Controls.Add(rfvTestDate);

            editor.TextBoxControl.Attributes.Add("onkeyup",
                String.Format("CopyTextToLeftField(\"{0}\",\"{1}\")",
                editor.TextBoxControl.ClientID, editor2.TextBoxControl.ClientID));

        }
    }


    protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
    {
        if (e.CommandName == "RowClick") 
        {
            List<string> list = new List<string>();
            for (int i = 0; i < RadGrid1.SelectedIndexes.Count; i++)
            {
                list.Add(RadGrid1.SelectedIndexes[i].ToString());
            }
            CustomSelectedIndexes = list;
           
            RadGrid1.Rebind();
        }


    }
    protected void RadGrid1_PreRender1(object sender, EventArgs e)
    {
        foreach (var value in CustomSelectedIndexes)
        {
            RadGrid1.Items[value].Selected = true;
        }
    }
}
