前台:
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div id="divshow" runat="server" >
</div>
</form>
</body>
</html>
后台:
GridView gvshow=null;
public void setBind()
{
DataTable dt = getDataTable(); //获得数据源
gvshow = new GridView();
gvshow.Width = Unit.Pixel(800);
gvshow.AutoGenerateColumns = false;
gvshow.RowDataBound += new GridViewRowEventHandler(gvshow_RowDataBind);
gvshow.RowDeleting +=new GridViewDeleteEventHandler(gvshow_RowDeleting);
gvshow.DataSource = dt;
for (int i = 0; i < dt.Columns.Count; i++)
{
BoundField bc = new BoundField();
bc.DataField = dt.Columns[i].ColumnName.ToString();
for (int i = 0; i < dt.Columns.Count; i++)
{
BoundField bc = new BoundField();
bc.DataField = dt.Columns[i].ColumnName.ToString();
bc.HeaderText = dt.Columns[i].Caption.ToString();
gvshow.Columns.Add(bc);
}
gvshow.Columns.Add(bc);
}
gvshow.RowStyle.BackColor = System.Drawing.Color.FromName("#E6EAF7");
gvshow.SelectedRowStyle.BackColor = System.Drawing.Color.FromName("#CE5D5A");
gvshow.SelectedRowStyle.ForeColor=System.Drawing.Color.White;
gvshow.SelectedRowStyle.Font.Bold=true;
gvshow.PagerStyle.BackColor = System.Drawing.Color.FromName("#F7F7DE");
gvshow.PagerStyle.ForeColor = System.Drawing.Color.Black;
gvshow.PagerStyle.HorizontalAlign = HorizontalAlign.Right;
gvshow.HeaderStyle.BackColor = System.Drawing.Color.LemonChiffon;
gvshow.HeaderStyle.ForeColor=System.Drawing.Color.FromName("#666699");
gvshow.HeaderStyle.HorizontalAlign=HorizontalAlign.Left;
gvshow.AlternatingRowStyle.BackColor = System.Drawing.Color.White;
gvshow.CellPadding = 4;
gvshow.Font.Size = FontUnit.Point(9);
gvshow.DataBind();
divshow.Controls.Add(gvshow);
}
void gvshow_RowDataBind(object sender, GridViewRowEventArgs e)
{
}
void gvshow_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int i = e.RowIndex;
Response.Write("delete:" + gvshow.Rows[i].Cells[0].Text.ToString());
}
public DataTable getDataTable(string idd)
{
string _sqlStr = "select * from test";
using (DBManage db = new DBManage())
{
DataTable dt = db.GetDataSet(_sqlStr).Tables[0];
return dt;
}
}
文章出处:http://www.diybl.com/course/4_webprogram/asp.net/netjs/2008520/117330.html