using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Routing;
using System.Data;
using Telerik.Web.UI;
using System.Web.Mvc;
using System.Collections.Specialized;
using System.Globalization;

public partial class _Default : System.Web.Mvc.ViewPage
{
    protected class ControllerMock : Controller
    {
        public ControllerMock(NameValueCollection queryString)
        {
            foreach (string param in queryString.Keys)
                ValueProvider.Add(
                    new KeyValuePair<string, ValueProviderResult>
                        (param, new ValueProviderResult(
                            queryString.Get(param), null,
                            CultureInfo.CurrentCulture)));
        }
    }

    protected void Page_Init(object sender, EventArgs e)
    {
        var contextWrapper = new HttpContextWrapper(HttpContext.Current);

        var controllerMock = new ControllerMock(Request.QueryString);

        var routeData = new RouteData();

        this.ViewContext = new ViewContext()
        {
            Controller = controllerMock,
            HttpContext = contextWrapper,
            RequestContext = new RequestContext(contextWrapper, routeData)
        };

        this.InitHelpers();
    }

    protected IEnumerable<string[]> GenerateData()
    {
        for (var i = 0; i < 100; i++)
            yield return new string[] { i.ToString(), "StringCol1 Row" + i };
    }

    protected void RadGrid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        DataTable dt = new DataTable();
        DataRow dr;
        int rowsNum = 100;

        string[][] colsInfo = {
			new string[] { "ID", "number" },
			new string[] { "PrimaryID", "number" },
			new string[] { "GroupID", "number" },
			new string[] { "HierarchyID", "number" },
			new string[] { "StringCol1", "string" },
			new string[] { "StringCol2", "string" },
			new string[] { "HyperLinkCol", "string" },
			new string[] { "DateCol", "date" },
			new string[] { "BoolCol", "bool" }
		};

        for (int i = 0; i < colsInfo.Length; i++)
        {
            switch (colsInfo[i][1])
            {
                case "number":
                    dt.Columns.Add(new DataColumn(colsInfo[i][0], typeof(Int32)));
                    break;
                case "date":
                    dt.Columns.Add(new DataColumn(colsInfo[i][0], typeof(DateTime)));
                    break;
                case "bool":
                    dt.Columns.Add(new DataColumn(colsInfo[i][0], typeof(Boolean)));
                    break;
                default:
                    dt.Columns.Add(new DataColumn(colsInfo[i][0], typeof(String)));
                    break;
            }

        }

        for (int j = 1; j <= rowsNum; j++)
        {
            dr = dt.NewRow();

            for (int k = 0; k < colsInfo.Length; k++)
            {
                switch (colsInfo[k][1])
                {
                    case "string":
                        dr[colsInfo[k][0]] = String.Format("{0} Row{1}", colsInfo[k][0], j);
                        break;
                    case "number":
                        switch (colsInfo[k][0])
                        {
                            case "PrimaryID":
                                dr[colsInfo[k][0]] = j;
                                break;
                            case "GroupID":
                                dr[colsInfo[k][0]] = (j % 13);
                                break;
                            case "HierarchyID":
                                dr[colsInfo[k][0]] = j;
                                break;
                            default:
                                dr[colsInfo[k][0]] = j * 10;
                                break;
                        }
                        break;
                    case "date":
                        dr[colsInfo[k][0]] = DateTime.Now.ToShortDateString();
                        break;
                    case "bool":
                        dr[colsInfo[k][0]] = j % 2 == 1 ? true : false;
                        break;
                    default:
                        break;
                }
            }
            dt.Rows.Add(dr);
        }

        (sender as RadGrid).DataSource = dt;
    }
}
