循环添加列的问题- 可以学习下如何动态创建Table 然后绑定gridview

<%@ Page Language="C#" %>

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.Sql" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    protected
void Page_Load(object sender, EventArgs e)
    {
       
if (!IsPostBack)
        {
            using (DataTable dt
= new DataTable())
            {
                dt.Columns.Add(
"money");
                dt.Columns.Add(
"date");
                dt.Rows.Add(dt.NewRow());
                dt.Rows.Add(dt.NewRow());
                Session[
"dt"] = dt;
               
this.GridView1.DataSource = dt;
               
this.GridView1.DataBind();
            }
        }
    }

    protected
void LinkButton1_Click(object sender, EventArgs e)
    {
        using (DataTable dt
= (DataTable)Session["dt"])
        {
            dt.Rows.Add(dt.NewRow());
            Session[
"dt"] = dt;
           
this.GridView1.DataSource = dt;
           
this.GridView1.DataBind();
        }
    }

    protected
void Button1_Click(object sender, EventArgs e)
    {
        using (SqlConnection conn
= new SqlConnection(connectionString))
        {
            conn.Open();
            using (SqlDataAdapter da
= new SqlDataAdapter("SELECT * FROM table_name WHERE 1=2", conn))
            {
                using (SqlCommandBuilder cb
= new SqlCommandBuilder(da))
                {
                    using (DataTable dt
= (DataTable)Session["dt"])
                    {
                       
for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            dt.Rows[i][
0] = ((TextBox)this.GridView1.Rows[i].FindControl("TextBox1")).Text;
                            dt.Rows[i][
1] = ((TextBox)this.GridView1.Rows[i].FindControl("TextBox2")).Text;
                        }
                        da.Update(dt);
                        dt.AcceptChanges();
                        Session[
"dt"] = dt;
                       
this.GridView1.DataSource = dt;
                       
this.GridView1.DataBind();
                    }
                }
            }
            conn.Close();
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
   
<title></title>
</head>
<body>
   
<form id="form1" runat="server">
       
<div>
           
<asp:Label ID="lblTheme" runat="server" Text="订单对应主题:"></asp:Label>
           
<asp:Label ID="lblCode" runat="server" Text="订单号:"></asp:Label><br />
           
<asp:Label ID="lblSum" runat="server" Text="总金额:"></asp:Label>
           
<asp:LinkButton ID="LinkButton1" runat="server" Text="添加更多" OnClick="LinkButton1_Click"></asp:LinkButton>
           
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" ShowHeader="false">
               
<Columns>
                   
<asp:TemplateField>
                       
<ItemTemplate>
                           
<asp:Label ID="Label1" runat="server"><%#Container.DataItemIndex + 1%>次回款金额:</asp:Label>
                       
</ItemTemplate>
                   
</asp:TemplateField>
                   
<asp:TemplateField>
                       
<ItemTemplate>
                           
<asp:TextBox ID="TextBox1" runat="server" Text="<%#Bind('money')%>"></asp:TextBox>
                       
</ItemTemplate>
                   
</asp:TemplateField>
                   
<asp:TemplateField>
                       
<ItemTemplate>
                           
<asp:Label ID="Label2" runat="server">回款时间:</asp:Label>
                       
</ItemTemplate>
                   
</asp:TemplateField>
                   
<asp:TemplateField>
                       
<ItemTemplate>
                           
<asp:TextBox ID="TextBox2" runat="server" Text="<%#Bind('date')%>"></asp:TextBox>
                       
</ItemTemplate>
                   
</asp:TemplateField>
               
</Columns>
           
</asp:GridView>
           
<asp:Button ID="Button1" runat="server" Text="提交" OnClick="Button1_Click" />
       
</div>
   
</form>
</body>
</html>

posted @ 2009-05-12 19:55  Daniel_Lu  阅读(510)  评论(0编辑  收藏  举报