日期:2014-05-18 浏览次数:20492 次
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CreateDataTable.aspx.cs" Inherits="CreateDataTable" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>动态生成DataTable</title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server"> </asp:GridView> </div> </form> </body> </html>
------解决方案--------------------
GridView中每一个
<asp:TemplateField HeaderText="。。。。">
<ItemTemplate>
<asp:Label ID="Label?" runat="server" Text="<%# Bind(。。。。) %>"></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TextBox?" runat="server" EnableViewState="false"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
在任意一个中加一个按钮作为添加
在响应时间中GridView1.FooterRow.FindControl依次找出各输入字段,然后在bind,试了一下没问题
------解决方案--------------------
protected void Button1_Click1(object sender, EventArgs e) { int num = 0; num = GridView1.Rows.Count; GridViewRow row = new GridViewRow(0, 0, DataControlRowType.EmptyDataRow, DataControlRowState.Normal); //添加第一个column TableCell cel1 = new TableCell(); cel1.Text = "1"; row.Cells.Add(cel1); //添加第二个column TableCell cel2 = new TableCell(); //在第二列添加一个textbox TextBox tx = new TextBox(); cel2.Controls.Add(tx); row.Cells.Add(cel2); //以此类推添加列...... GridView1.Controls[0].Controls.Add(row); }
------解决方案--------------------
例:
在没有数据时显示标题
<EmptyDataTemplate>
暂时没有数据!!!!
</EmptyDataTemplate>
动态添加行
DataTable dt = new DataTable();
dt.Columns.Add("WARE_ID", typeof(string));
dt.Columns.Add("WARE_NAME", typeof(string));
dt.Columns.Add("WARE_TYPE", typeof(string));
dt.Columns.Add("WARE_JANMA", typeof(string));
dt.Columns.Add("WARE_UNIT", typeof(string));
dt.Columns.Add("WARE_SPECIFICATION", typeof(string));
dt.Columns.Add("WARE_COLOR", typeof(string));
dt.Columns.Add("WARE_STOCK", typeof(string));
dt.Columns.Add("WARE_PRICE_JJ", typeof(string));
dt.Columns.Add("WARE_PRICE_SJ", typeof(string));
dt.Columns.Add("WARE_MANUFACTURER", typeof(string));
dt.Columns.Add("WARE_NOTE", typeof(string));
for (int i = 0; i < this.GridView1.Rows.Count; i++)
{
string str_WARE_ID = ((TextBox)this.GridView1.Rows[i].FindControl("TextBox2")).Text.Trim();
string str_WARE_NAME = ((TextBox)this.GridView1.Rows[i].FindControl("TextBox3")).Text.Trim();
string str_WARE_TYPE = ((TextBox)this.GridView1.