绑定透视表(行转列)

绑定透视表(行转列)_第1张图片

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 connection1 = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("~/Grid/Data/Access/Nwind.mdb"));
       OleDbDataAdapter adapter1 = new OleDbDataAdapter();
       adapter1.SelectCommand = new OleDbCommand(query, connection1);
       DataTable table1;
       connection1.Open();
       try
       {
           adapter1.Fill(table1);
       }
       finally
       {
           connection1.Close();
       }
       return table1;
}


你可能感兴趣的:(ASP.NET,Telerik.Web.UI)