using C1.Web.Wijmo.Controls.C1GridView;
using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace GridView_Pagination
{
    public partial class _Default : Page
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            BuildColumns();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                BindGrid();
        }

        void BuildColumns()
        {
            var g = ConfigurationParamGrid;
            g.Columns.Clear();

            // ID — no thousands separator (4002 not 4,002)
            g.Columns.Add(CreateTemplateField("ID", "ID", "{0:0}", 80));

            g.Columns.Add(CreateBoundField("Name", "Customer Name", 200));
            g.Columns.Add(CreateBoundField("Product", "Product Purchased", 250));
            g.Columns.Add(CreateBoundField("Region", "Region", 150));
            g.Columns.Add(CreateBoundField("Status", "Order Status", 150));

            // Date — formatted
            g.Columns.Add(CreateTemplateField("Date", "Order Date",
                "{0:dd/MM/yyyy HH:mm:ss}", 180));

            // Total — currency
            g.Columns.Add(CreateTemplateField("Total", "Total Amount",
                "{0:C}", 150));
        }

        C1BoundField CreateBoundField(string dataField, string header, int width)
        {
            return new C1BoundField
            {
                DataField = dataField,
                HeaderText = header,
                SortExpression = dataField,
                ItemStyle = { Width = Unit.Pixel(width) }
            };
        }

        C1TemplateField CreateTemplateField(string dataField,
                                           string header,
                                           string format,
                                           int width)
        {
            var f = new C1TemplateField
            {
                DataField = dataField,
                HeaderText = header,
                SortExpression = dataField,
                ItemStyle = { Width = Unit.Pixel(width) },
                ItemTemplate = new EvalFormatTemplate(dataField, format)
            };
            return f;
        }

        class EvalFormatTemplate : ITemplate
        {
            string _field;
            string _format;

            public EvalFormatTemplate(string field, string format)
            {
                _field = field;
                _format = format;
            }

            public void InstantiateIn(Control container)
            {
                var lit = new Literal();
                lit.DataBinding += (s, e) =>
                {
                    var row = (C1GridViewRow)lit.NamingContainer;
                    var val = DataBinder.Eval(row.DataItem, _field);
                    lit.Text = val == null ? "" :
                        string.Format(_format, val);
                };
                container.Controls.Add(lit);
            }
        }

        void BindGrid()
        {
            var dt = new DataTable();

            dt.Columns.Add("ID", typeof(int));
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Product", typeof(string));
            dt.Columns.Add("Region", typeof(string));
            dt.Columns.Add("Status", typeof(string));
            dt.Columns.Add("Date", typeof(DateTime));
            dt.Columns.Add("Total", typeof(decimal));

            for (int i = 1; i <= 50; i++)
            {
                dt.Rows.Add(
                    4000 + i,
                    "Customer " + i,
                    "Very Long Product Description " + i,
                    "North America",
                    "Shipped",
                    DateTime.Now.AddHours(13 * i),   // real DateTime
                    500.00m                           // real decimal
                );
            }

            ConfigurationParamGrid.DataSource = dt;
            ConfigurationParamGrid.DataBind();
        }

        protected void ConfigurationParamGrid_PageIndexChanging(
            object sender, C1GridViewPageEventArgs e)
        {
            ConfigurationParamGrid.PageIndex = e.NewPageIndex;
            BindGrid();
        }
    }
}
