using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Telerik.Web.UI;
using System.Data.OleDb;
using System.Collections;
using Telerik.Web.UI.Calendar;

public partial class _Default : System.Web.UI.Page
{
    public DataTable DataSource
    {
        get
        {
            object res = this.Session["_ds"];
            if (res == null)
            {
                OleDbConnection MyOleDbConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + System.Web.HttpContext.Current.Server.MapPath("~/App_Data/Nwind.mdb"));
                OleDbDataAdapter MyOleDbDataAdapter = new OleDbDataAdapter();
                MyOleDbDataAdapter.SelectCommand = new OleDbCommand("SELECT top 20 * FROM Customers", MyOleDbConnection);

                DataTable table = new DataTable();

                MyOleDbConnection.Open();
                try
                {
                    MyOleDbDataAdapter.Fill(table);
                }
                finally
                {
                    MyOleDbConnection.Close();
                }

                this.Session["_ds"] = table;
            }

            return (DataTable)this.Session["_ds"];
        }
    }

    protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
        RadGrid1.DataSource = this.DataSource;
    }

    protected override void RaisePostBackEvent(IPostBackEventHandler sourceControl, string eventArgument)
    {
        base.RaisePostBackEvent(sourceControl, eventArgument);
        try
        {
            if (sourceControl is RadAjaxManager)
            {
                string[] eventArgumentData = eventArgument.Split(',');
                if (eventArgumentData[0] == "RowMoved")
                {
                    GridItem movedItem = (RadGrid1.MasterTableView.Items[int.Parse(eventArgumentData[1])] as GridDataItem);
                    GridItem beforeItem = (RadGrid1.MasterTableView.Items[int.Parse(eventArgumentData[2])] as GridDataItem);

                    object key1 = RadGrid1.MasterTableView.DataKeyValues[movedItem.ItemIndex]["CustomerID"];
                    object key2 = RadGrid1.MasterTableView.DataKeyValues[beforeItem.ItemIndex]["CustomerID"];

                    DataRow row1 = this.DataSource.Select(String.Format("CustomerID = '{0}'", key1.ToString()))[0];
                    DataRow row2 = this.DataSource.Select(String.Format("CustomerID = '{0}'", key2.ToString()))[0];

                    row1.BeginEdit();
                    row2.BeginEdit();

                    //switch the CustomerIDs for the swapped grid rows
                    row2["CustomerID"] = key1;
                    row1["CustomerID"] = key2;

                    //you should perform the same operation for the rest of the rows cells content
                    //before finishiing the edit operation

                    row1.EndEdit();
                    row2.EndEdit();

                    row1.AcceptChanges();
                    row2.AcceptChanges();

                    RadGrid1.Rebind();

                    
                }
            }
        }
        catch (Exception ex)
        {

        }
    }
}
