using System;
using System.Data;
using System.Data.OleDb;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Web.UI;
using System.Collections;

public partial class _Default : System.Web.UI.Page
{
    public DataTable GetDataTable(string query)
    {
        OleDbConnection MyOleDbConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("~/Nwind.mdb"));
        OleDbDataAdapter MyOleDbDataAdapter = new OleDbDataAdapter();
        MyOleDbDataAdapter.SelectCommand = new OleDbCommand(query, MyOleDbConnection);

        DataTable myDataTable = new DataTable();

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

        return myDataTable;
    }

    public DataTable PivotTable(DataTable source)
    {
        DataTable dest = new DataTable("Pivoted" + source.TableName);

        dest.Columns.Add(" ");

        foreach (DataRow r in source.Rows)
            dest.Columns.Add(r[0].ToString());

        for (int i = 0; i < source.Columns.Count - 1; i++)
        {
            dest.Rows.Add(dest.NewRow());
        }

        for (int r = 0; r < dest.Rows.Count; r++)
        {
            for (int c = 0; c < dest.Columns.Count; c++)
            {
                if (c == 0)
                    dest.Rows[r][0] = source.Columns[r + 1].ColumnName;
                else
                    dest.Rows[r][c] = source.Rows[c - 1][r + 1];
            }
        }
        dest.AcceptChanges();
        return dest;
    }

    protected void RadGrid1_NeedDataSource1(object source, GridNeedDataSourceEventArgs e)
    {
        RadGrid1.DataSource = PivotTable(GetDataTable("SELECT TOP 15 CustomerID, CompanyName, ContactName, ContactTitle, Address, PostalCode, City, Country  FROM Customers"));
    }
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridHeaderItem)
        {
            for (int i = 1; i < e.Item.OwnerTableView.AutoGeneratedColumns.Length; i++)
            {
                GridBoundColumn column = (GridBoundColumn)e.Item.OwnerTableView.AutoGeneratedColumns[i];
                TableCell cell = ((GridHeaderItem)e.Item)[column.UniqueName];

                LinkButton LinkButton1 = new LinkButton();
                LinkButton1.ID = "Edit" + column.UniqueName;
                LinkButton1.Text = "Edit";
                LinkButton1.CommandName = "MyCustomEdit";
                LinkButton1.CommandArgument = column.UniqueName;

                cell.Controls.Add(new LiteralControl("&nbsp;"));
                cell.Controls.Add(LinkButton1);

                LinkButton1 = new LinkButton();
                LinkButton1.ID = "Cancel" + column.UniqueName;
                LinkButton1.Text = "Cancel";
                LinkButton1.CommandName = "MyCustomCancel";
                LinkButton1.CommandArgument = column.UniqueName;
                LinkButton1.Visible = false;

                cell.Controls.Add(new LiteralControl("&nbsp;"));
                cell.Controls.Add(LinkButton1);

                LinkButton1 = new LinkButton();
                LinkButton1.ID = "Update" + column.UniqueName;
                LinkButton1.Text = "Update";
                LinkButton1.CommandName = "MyCustomUpdate";
                LinkButton1.CommandArgument = column.UniqueName;
                LinkButton1.Visible = false;

                cell.Controls.Add(new LiteralControl("&nbsp;"));
                cell.Controls.Add(LinkButton1);
            }
        }

        if (e.Item is GridDataItem)
        {
            (e.Item as GridDataItem)[(RadGrid1.MasterTableView.AutoGeneratedColumns[0] as GridBoundColumn).UniqueName].Font.Bold = true;
            (e.Item as GridDataItem)[(RadGrid1.MasterTableView.AutoGeneratedColumns[0] as GridBoundColumn).UniqueName].BackColor = System.Drawing.SystemColors.Control;
            (e.Item as GridDataItem)[(RadGrid1.MasterTableView.AutoGeneratedColumns[0] as GridBoundColumn).UniqueName].BorderColor = System.Drawing.Color.White;
        }
    }

    protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
    {
        if (e.CommandName == "MyCustomEdit")
        {
            for (int i = 0; i < e.Item.OwnerTableView.AutoGeneratedColumns.Length; i++)
            {
                GridBoundColumn column = (GridBoundColumn)e.Item.OwnerTableView.AutoGeneratedColumns[i];

                column.ReadOnly = (column.UniqueName == e.CommandArgument) ? false : true;
            }

            foreach (GridDataItem item in RadGrid1.Items)
            {
                item.Edit = true;
            }
            RadGrid1.Rebind();

            GridHeaderItem headerItem = (GridHeaderItem)e.Item.OwnerTableView.GetItems(GridItemType.Header)[0];
            for (int i = 1; i < e.Item.OwnerTableView.AutoGeneratedColumns.Length; i++)
            {
                GridBoundColumn column = (GridBoundColumn)e.Item.OwnerTableView.AutoGeneratedColumns[i];

                if (column.UniqueName != e.CommandArgument)
                    continue;

                LinkButton LinkButton1 = (LinkButton)headerItem.FindControl("Edit" + column.UniqueName);
                LinkButton1.Visible = false;

                LinkButton1 = (LinkButton)headerItem.FindControl("Cancel" + column.UniqueName);
                LinkButton1.Visible = true;

                LinkButton1 = (LinkButton)headerItem.FindControl("Update" + column.UniqueName);
                LinkButton1.Visible = true;
            }
        }

        if (e.CommandName == "MyCustomCancel")
        {
            foreach (GridDataItem item in RadGrid1.Items)
            {
                item.Edit = false;
            }
            RadGrid1.Rebind();

            GridHeaderItem headerItem = (GridHeaderItem)e.Item.OwnerTableView.GetItems(GridItemType.Header)[0];
            for (int i = 1; i < e.Item.OwnerTableView.AutoGeneratedColumns.Length; i++)
            {
                GridBoundColumn column = (GridBoundColumn)e.Item.OwnerTableView.AutoGeneratedColumns[i];

                if (column.UniqueName != e.CommandArgument)
                    continue;

                LinkButton LinkButton1 = (LinkButton)headerItem.FindControl("Edit" + column.UniqueName);
                LinkButton1.Visible = true;

                LinkButton1 = (LinkButton)headerItem.FindControl("Cancel" + column.UniqueName);
                LinkButton1.Visible = false;

                LinkButton1 = (LinkButton)headerItem.FindControl("Update" + column.UniqueName);
                LinkButton1.Visible = false;
            }
        }

        if (e.CommandName == "MyCustomUpdate")
        {
            string editColumnUniqueName = "";

            GridHeaderItem headerItem = (GridHeaderItem)e.Item.OwnerTableView.GetItems(GridItemType.Header)[0];
            for (int i = 1; i < e.Item.OwnerTableView.AutoGeneratedColumns.Length; i++)
            {
                GridBoundColumn column = (GridBoundColumn)e.Item.OwnerTableView.AutoGeneratedColumns[i];

                if (column.UniqueName != e.CommandArgument)
                    continue;

                editColumnUniqueName = column.UniqueName;

                LinkButton LinkButton1 = (LinkButton)headerItem.FindControl("Edit" + column.UniqueName);
                LinkButton1.Visible = true;

                LinkButton1 = (LinkButton)headerItem.FindControl("Cancel" + column.UniqueName);
                LinkButton1.Visible = false;

                LinkButton1 = (LinkButton)headerItem.FindControl("Update" + column.UniqueName);
                LinkButton1.Visible = false;
            }

            Hashtable newValues = new Hashtable();
            newValues["CustomerID"] = ((LiteralControl)headerItem[editColumnUniqueName].Controls[0]).Text;
            for (int i = 0; i < RadGrid1.Items.Count; i++)
            {
                if (i > 0)
                {
                    newValues[RadGrid1.Items[i].Cells[2].Text] = ((TextBox)RadGrid1.Items[i][editColumnUniqueName].Controls[0]).Text;
                }
                RadGrid1.Items[i].Edit = false;
            }
            RadGrid1.Rebind();


            foreach (DictionaryEntry entry in newValues)
            {
                Label1.Text = String.Format("{0} : {1} <br />", entry.Key, entry.Value);
            }
        }
    }
}
