asp.net 程序动态添加gridview

前台:
<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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值