using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Sorting : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void RadGrid1_NeedDataSource(object sender, EventArgs e)
    {
        RadGrid1.DataSource = SampleData;
    }

    public DataTable SampleData
    {
        get
        {
            if (Session["_data"] == null || !Page.IsPostBack)
            {
                System.Data.DataTable table = new System.Data.DataTable();
                table.Columns.Add("ID", Type.GetType("System.Int32"));
                table.Columns.Add("Name", Type.GetType("System.String"));
                table.Columns.Add("Date", Type.GetType("System.DateTime"));
                table.Columns.Add("Value", Type.GetType("System.Double"));
                table.Columns.Add("Order", Type.GetType("System.Int32"));

                Random rand = new Random();

                for (int i = 0; i < 10; i++)
                {
                    table.Rows.Add(i,
                                   "Item " + i.ToString(),
                                   DateTime.Today.AddDays(-10).AddDays(rand.NextDouble() * 20),
                                   rand.NextDouble() * 1000,
                                   i);
                }

                Session["_data"] = table;
            }

            DataTable original = (DataTable)Session["_data"];
            DataTable ret = original.Clone();
            DataRow[] sortedRows = original.Select("", "Order ASC");
            foreach (DataRow row in sortedRows)
            {
                ret.Rows.Add(row.ItemArray);
            }

            return ret;
        }
    }
}
