using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Telerik.Web.UI;
using System.Data.SqlClient;

public partial class Default : System.Web.UI.Page 
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);

        RadGrid1.NeedDataSource += new GridNeedDataSourceEventHandler(RadGrid1_NeedDataSource);
        RadGrid1.ItemDataBound += new GridItemEventHandler(RadGrid1_ItemDataBound);
        RadGrid1.ItemCommand += new GridCommandEventHandler(RadGrid1_ItemCommand);
        RadGrid1.ItemCreated += new GridItemEventHandler(RadGrid1_ItemCreated);
    }

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
    }

    void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem item = e.Item as GridDataItem;           
            LinkButton button = new LinkButton();
            button.ID = "MyButton";
            item["ALFKI"].Controls.Add(button);
        }
    }

    void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == "customCommand")
        {
            RadAjaxManager1.Alert(e.CommandArgument.ToString());
        }
    }

    void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem item = e.Item as GridDataItem;
            string id = DataBinder.Eval(item.DataItem, "ALFKI").ToString();
            LinkButton button = new LinkButton();
            button.ID = "MyButton";
            item["ALFKI"].Controls.Add(button);
            button.Text = id;
            button.CommandName = "customCommand";
            button.CommandArgument = id;            
        }
    }

    void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        RadGrid1.DataSource = PivotTable(GetDataTable("SELECT TOP 7 CustomerID, CompanyName, ContactName, ContactTitle, Address, PostalCode, City, Country FROM Customers"));
    }

    public DataTable GetDataTable(string query)
    {
        String ConnString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
        SqlConnection conn = new SqlConnection(ConnString);
        SqlDataAdapter adapter = new SqlDataAdapter();
        adapter.SelectCommand = new SqlCommand(query, conn);

        DataTable myDataTable = new DataTable();

        conn.Open();
        try
        {
            adapter.Fill(myDataTable);
        }
        finally
        {
            conn.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;
    }
}
