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;

public partial class _Default : System.Web.UI.Page
{
    RadGrid grid;
    private void createGrid()
    {
        grid = new RadGrid();
        setGridProperties(grid);
        _Default.AddDateTimeFieldToGridView("Date", "Date", grid, null);
    }

    public static void AddDateTimeFieldToGridView(string fieldName, string headerText, RadGrid grid, int? width)
    {
        GridDateTimeColumn dtc = new GridDateTimeColumn()
        {
            HeaderText = headerText,
            DataField = fieldName
        };
        dtc.SortExpression = fieldName.Trim();
        dtc.AllowSorting = true;
        dtc.Resizable = true;
        dtc.UniqueName = fieldName;
        dtc.DataFormatString = "{0:dd.MM.yyyy}";
        dtc.PickerType = GridDateTimeColumnPickerType.DateTimePicker;
        dtc.UniqueName = fieldName;
        if (width != null)
        {
            dtc.HeaderStyle.Width = width.Value;
        }
        grid.Columns.Add(dtc);
    }

    private void setGridProperties(RadGrid grid)
    {
        grid.ID = "rgWorkflows";
        grid.EnableViewState = true;
        grid.AutoGenerateColumns = false;
        grid.AllowSorting = true;
        grid.AllowPaging = true;
        grid.AllowFilteringByColumn = false;
        grid.GridLines = GridLines.None;
        grid.Width = Unit.Percentage(99.8);
        grid.Height = Unit.Percentage(299.8);
        grid.HeaderStyle.Width = Unit.Pixel(150);
        grid.AllowMultiRowSelection = true;
        grid.MasterTableView.PagerStyle.Mode = GridPagerMode.NextPrevNumericAndAdvanced;
        grid.ClientSettings.EnableRowHoverStyle = true;
        //grid.ClientSettings.Selecting.AllowRowSelect = true;
        //grid.ClientSettings.Scrolling.AllowScroll = true;
        //grid.ClientSettings.Scrolling.ScrollHeight = new Unit(400);
        //grid.ClientSettings.Scrolling.UseStaticHeaders = true;
        //grid.ClientSettings.Scrolling.SaveScrollPosition = true;
        grid.ClientSettings.Resizing.AllowColumnResize = true;
        grid.ClientSettings.Resizing.ClipCellContentOnResize = false;
        grid.ClientSettings.Resizing.EnableRealTimeResize = true;
        grid.ClientSettings.Resizing.ResizeGridOnColumnResize = true;
        grid.MasterTableView.DataKeyNames = new string[] { "Id" };
        grid.MasterTableView.OverrideDataSourceControlSorting = true;
        grid.PagerStyle.AlwaysVisible = true;
        if (!IsPostBack)
        {
            grid.MasterTableView.PageSize = 15;
        }
        grid.GroupingSettings.CaseSensitive = false;
        grid.MasterTableView.PagerStyle.Width = 1000;
        grid.MasterTableView.OverrideDataSourceControlSorting = true;
        grid.MasterTableView.EnableColumnsViewState = false;
        grid.AllowFilteringByColumn = true;

        grid.AllowMultiRowSelection = false;
    }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        createGrid();
        grid.NeedDataSource += new GridNeedDataSourceEventHandler(grid_NeedDataSource);
        Panel1.Controls.Add(grid);
    }

    void grid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        DataTable table = new DataTable();
        table.Columns.Add("ID");
        table.Columns.Add("Date", typeof(DateTime));
        for (int i = 1; i < 8; i++)
        {
            table.Rows.Add(i, DateTime.Now.AddDays(i));
        }
        grid.DataSource = table;
    }
}
