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

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

    }
    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 7 CustomerID, CompanyName, ContactName, ContactTitle, Address, PostalCode, City, Country FROM Customers"));
    }
    public DataTable GetDataTable(String query)
    {
        OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("~/App_Data/Nwind.mdb"));
        OleDbDataAdapter adapter = new OleDbDataAdapter();
        adapter.SelectCommand = new OleDbCommand(query, conn);
        DataTable table1 = new DataTable();
        conn.Open();
        try
        {
            adapter.Fill(table1);
        }
        finally
        {
            conn.Close();
        }
        return table1;
    }
    protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
        RadGrid1.MasterTableView.RenderColumns[2].HeaderStyle.Width = Unit.Percentage(15);
        RadGrid1.Rebind();
    }

}
