using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.Data.SqlClient; namespace net { /// <summary> /// DataGrid_valign 的摘要说明。 /// </summary> public class DataGrid_valign : System.Web.UI.Page { protected System.Web.UI.WebControls.Table Table1; protected System.Web.UI.WebControls.Button Button1; protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e) { // 初始化页面 SqlConnection Conn = new SqlConnection(); string connStr = "server=hm;database=Northwind;user id=sa;password="; Conn.ConnectionString = connStr; string sqlStr = "select EmployeeID,LastName,FirstName,TitleOfCourtesy from Employees"; SqlDataAdapter cmd2 = new SqlDataAdapter(sqlStr,Conn); DataSet ds = new DataSet(); cmd2.Fill(ds,"table1");; //数据绑定 DataGrid1.DataSource = ds.Tables["table1"].DefaultView; DataGrid1.DataBind(); } #region Web 窗体设计器生成的代码 override protected void OnInit(EventArgs e) { // // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); }
/// /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// private void InitializeComponent() { this.Button1.Click += new System.EventHandler(this.Button1_Click); this.Load += new System.EventHandler(this.Page_Load); } #endregion private void Button1_Click(object sender, System.EventArgs e) { // 初始化页面 SqlConnection Conn = new SqlConnection(); string connStr = "server=hm;database=Northwind;user id=sa;password="; Conn.ConnectionString = connStr; string sqlStr = "select EmployeeID,LastName,FirstName,TitleOfCourtesy from Employees"; SqlDataAdapter cmd2 = new SqlDataAdapter(sqlStr,Conn); DataSet ds = new DataSet(); cmd2.Fill(ds,"table1");; //数据绑定 DataGrid1.DataSource = ds.Tables["table1"].DefaultView; DataGrid1.DataBind(); foreach (DataColumn dc in ds.Tables["table1"].Columns) { //转换数据视图 TableRow trow = new TableRow(); TableCell tcell = new TableCell(); foreach (DataRow dr in ds.Tables["table1"].Rows) { //循环添加标题 tcell.Text = dc.ColumnName; trow.BackColor = System.Drawing.Color.LemonChiffon; tcell.BackColor = System.Drawing.Color.LightGreen; tcell.Controls.Add(new LiteralControl(dc.ColumnName.ToString())); trow.Cells.Add(tcell); } foreach (DataRow dr in ds.Tables["table1"].Rows ) { //循环添加表格数据 TableCell tcelldata = new TableCell(); //创建新行(数据行) tcelldata.Controls.Add(new LiteralControl(dr[dc.ColumnName].ToString())); trow.Cells.Add(tcelldata); } Table1.Rows.Add(trow); //添加表格行(包括标题和数据的行) } } } }
新建一个DataTable,然后倒数据咯~
如果在DataSet里,大量循环操作可能难以避免了.
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace net
{
/// <summary>
/// DataGrid_valign 的摘要说明。
/// </summary>
public class DataGrid_valign : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Table Table1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
// 初始化页面
SqlConnection Conn = new SqlConnection();
string connStr = "server=hm;database=Northwind;user id=sa;password=";
Conn.ConnectionString = connStr;
string sqlStr = "select EmployeeID,LastName,FirstName,TitleOfCourtesy from Employees";
SqlDataAdapter cmd2 = new SqlDataAdapter(sqlStr,Conn);
DataSet ds = new DataSet();
cmd2.Fill(ds,"table1");;
//数据绑定
DataGrid1.DataSource = ds.Tables["table1"].DefaultView;
DataGrid1.DataBind();
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void Button1_Click(object sender, System.EventArgs e)
{
// 初始化页面
SqlConnection Conn = new SqlConnection();
string connStr = "server=hm;database=Northwind;user id=sa;password=";
Conn.ConnectionString = connStr;
string sqlStr = "select EmployeeID,LastName,FirstName,TitleOfCourtesy from Employees";
SqlDataAdapter cmd2 = new SqlDataAdapter(sqlStr,Conn);
DataSet ds = new DataSet();
cmd2.Fill(ds,"table1");;
//数据绑定
DataGrid1.DataSource = ds.Tables["table1"].DefaultView;
DataGrid1.DataBind(); foreach (DataColumn dc in ds.Tables["table1"].Columns)
{
//转换数据视图
TableRow trow = new TableRow();
TableCell tcell = new TableCell();
foreach (DataRow dr in ds.Tables["table1"].Rows)
{
//循环添加标题
tcell.Text = dc.ColumnName;
trow.BackColor = System.Drawing.Color.LemonChiffon;
tcell.BackColor = System.Drawing.Color.LightGreen;
tcell.Controls.Add(new LiteralControl(dc.ColumnName.ToString()));
trow.Cells.Add(tcell);
}
foreach (DataRow dr in ds.Tables["table1"].Rows )
{
//循环添加表格数据
TableCell tcelldata = new TableCell(); //创建新行(数据行)
tcelldata.Controls.Add(new LiteralControl(dr[dc.ColumnName].ToString()));
trow.Cells.Add(tcelldata);
}
Table1.Rows.Add(trow); //添加表格行(包括标题和数据的行)
}
}
}
}
就像楼上上说的用repeater实现.如果一定要用dataset也是可以的.
用datalist切换排列方式即可