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.SqlClient;

public partial class _Default : System.Web.UI.Page 
{
    private int TotalDataSourceRows
    {
        get
        {
            return ViewState["TotalDataSourceRows"] == null ? 0 : (int)ViewState["TotalDataSourceRows"];
        }
        set
        {
            if (value != 0)
                ViewState["TotalDataSourceRows"] = value;
        }
    }

    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());

        source.Columns.Add("Accept");
        source.Columns.Add("Reject");

        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)
    {
        DataTable dt = PivotTable(GetDataTable("SELECT TOP 5 CustomerID, CompanyName, ContactName, ContactTitle, Address, PostalCode, City, Country FROM Customers"));
        TotalDataSourceRows = dt.Rows.Count;
        RadGrid1.DataSource = dt;
    }   

    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
    {
        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;

            if (e.Item.ItemIndex >= TotalDataSourceRows - 2)
            {
                GridDataItem item = (e.Item as GridDataItem);
                for(int i = 3;i<RadGrid1.MasterTableView.RenderColumns.Length;i++)
                {
                    GridColumn column = RadGrid1.MasterTableView.RenderColumns[i];
                    if (e.Item.ItemIndex == TotalDataSourceRows - 2)
                        item[column].Controls.Add(CreateButtonInCell(column.UniqueName, "Accept", "Accept"));
                    else
                        item[column].Controls.Add(CreateButtonInCell(column.UniqueName, "Reject", "Reject"));
                }
            }
        }
    }

    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem && e.Item.ItemIndex >= TotalDataSourceRows - 2)
        {
            GridDataItem item = (e.Item as GridDataItem);
            for (int i = 3; i < RadGrid1.MasterTableView.RenderColumns.Length; i++)
            {
                GridColumn column = RadGrid1.MasterTableView.RenderColumns[i];
                if (e.Item.ItemIndex == TotalDataSourceRows - 2)
                    item[column].Controls.Add(CreateButtonInCell(column.UniqueName, "Accept", "Accept"));
                else
                    item[column].Controls.Add(CreateButtonInCell(column.UniqueName, "Reject", "Reject"));
            }
        }
    }

    protected LinkButton CreateButtonInCell(string ColumnName, string ButtonText, string CommandName)
    {
        LinkButton button = new LinkButton();
        button.ID = ColumnName + CommandName;
        button.CommandName = CommandName;
        button.CommandArgument = ColumnName;
        button.Text = ButtonText;

        return button;
    }

    protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
    {
        if (e.CommandName == "Accept")
        {
            //your custom action here
        }
        else if (e.CommandName == "Reject")
        {
            //your custom action here
        }
    }    
  
}