asp.net 程序动态添加gridview
2009-10-25 16:34
411 查看
前台:
<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
<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
相关文章推荐
- ASP.NET 2.0中动态添加 GridView 模板列的例子
- ASP.NET 2.0 中动态添加 GridView 模板列
- ASP.NET 2.0 中动态添加 GridView 模板列 以及取值的例子
- ASP.NET中GridView动态添加下拉框
- ASP.NET 2.0 中动态添加 GridView 模板列的例子
- ASP.NET 2.0 中动态添加 GridView 模板列的例子
- 关于asp.net中gridview的问题,关于footer,16aspx上下的英语交流网程序,管理员的添加和修改有问题
- ASP.NET 2.0 中动态添加 GridView 模板列的例子
- ASP.NET 2.0中动态添加 GridView 模板列的例子
- ASP.NET 2.0 中动态添加 GridView 模板列 以及取值的例子
- ASP.NET 2.0 中动态添加 GridView 模板列的例子
- [导入]ASP.NET 2.0 中动态添加 GridView 模板列的例子
- asp.net gridview动态添加列,并获取其数据;
- ASP.NET 2.0 中动态添加 GridView 模板列 以及取值的例子
- ASP.NET 2.0中动态添加GridView
- ASP.NET 2.0 中动态添加 GridView 模板列的例子
- ASP.NET 2.0中动态添加 GridView 模板列的例子
- 在ASP.NET程序中用程序动态向<head>便签里添加<meta>标签
- ASP.NET 2.0 中动态添加 GridView 模板列的例子
- asp.net动态添加GridView的模板列,并获取列值