当前位置: 代码迷 >> ASP.NET >> 请问GridView添加行的有关问题
  详细解决方案

请问GridView添加行的有关问题

热度:6430   发布时间:2013-02-25 00:00:00.0
请教GridView添加行的问题
请教各位大哥:
  我想在一个不绑定数据的空GridView中插入行,插入行后,行里的数据是自己输入,如需添加在插入下一行,当数据插入。完毕以后再由一个Button将所有的数据一起提交到数据库中。
  注:希望能在GridView外有一个Button能控制行的增加。或者有什么好的方法也可以告诉我。感激不尽。


------解决方案--------------------------------------------------------
HTML code
<%@ 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,试了一下没问题


------解决方案--------------------------------------------------------
C# code
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.Rows[i].FindControl("TextBox4")).Text.Trim();