如何动态地向Gridview添加多行 [英] How to add multiple rows to a Gridview dynamically
本文介绍了如何动态地向Gridview添加多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在下面的代码中,我可以点击开始按钮将一行插入GView。我想知道如何通过点击添加新按钮插入新行。
任何人都可以指导我吗?
In the following code I am able to insert one row into a GView on click of 'begin' button. I would like to know how to insert new rows with the click of 'add new' button.
Can any one guide me ?
<asp:GridView ID="GridView1" runat="server" Font-Names="Arial" Font-Size="XX-Small"
Style="z-index: 118; left: 174px; position: absolute; top: 421px" Width="543px" Height="66px" EmptyDataText="--- Empty Grid ---" AllowPaging="True" Caption="Customer Status Report" CaptionAlign="Top" DataKeyNames="Item #" >
</asp:GridView>
<asp:textbox ID="Textbox1" runat="server"></asp:textbox>
<asp:textbox ID="Textbox2" runat="server"></asp:textbox>
<asp:Button ID="Button2" runat="server" Text="Begin" OnClick="Button2_Click" />
<asp:Button ID="Button3" runat="server" Text="Add new" OnClick="Button3_Click"
style="z-index: 1;font-family:Sans-Serif; height:22px; left:122px; margin-top:100px; font-weight:900; " />
protected void Button2_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Item #", typeof(int)));
dt.Columns.Add(new DataColumn("Customer Name", typeof(string)));
dt.Columns.Add(new DataColumn("Contract Number", typeof(string)));
DataRow dr = dt.NewRow();
dr["Item #"] = 1;// i;
dr["Customer Name"] = Textbox1.Text;
dr["Contract Number"] = Textbox2.Text;
dt.Rows.Add(dr);
this.GridView1.Visible = true;
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void Button3_Click(object sender, EventArgs e) //to add new rows to GV
{
}
推荐答案
请参阅此处,
http://www.aspsnippets.com/Articles/Adding- Dynamic-Rows-in-ASP.Net-GridView-Control-with-TextBoxes.aspx [ ^ ]
我想我的朋友这个shuold帮助你< br $>
protected void Button2_Click(object sender,EventArgs e)
{
DataTable dt = new DataTable();
//在dt fil中l首先你在网格视图中检索或显示/绑定的所有记录
比添加此代码//
dt.Columns.Add(new DataColumn(Item#,typeof(int)));
dt.Columns.Add(new DataColumn(Customer Name,typeof(string)));
dt.Columns.Add(new DataColumn(Contract Number,typeof(string)));
dr = dt.NewRow();
dr [Item#] = i;
dr [客户名称] =;
dr [合同号] =;
dt.Rows.Add(dr);
this.GridView1.Visible = true;
GridView1.DataSource = dt;
GridView1.DataBind();
}
Ithink my friend this shuold help you
protected void Button2_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
// in dt fill first all record which you retriv or shows/Bind in gridview
than add this code//
dt.Columns.Add(new DataColumn("Item #", typeof(int)));
dt.Columns.Add(new DataColumn("Customer Name", typeof(string)));
dt.Columns.Add(new DataColumn("Contract Number", typeof(string)));
dr = dt.NewRow();
dr["Item #"] = i;
dr["Customer Name"] = "";
dr["Contract Number"] ="";
dt.Rows.Add(dr);
this.GridView1.Visible = true;
GridView1.DataSource = dt;
GridView1.DataBind();
}
这篇关于如何动态地向Gridview添加多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文