在网格中创建动态行时如何插入数据 [英] how to insert data when created dynamic row in grid
本文介绍了在网格中创建动态行时如何插入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,
我正在使用网格并在单击添加行按钮时动态创建新行.所以现在如何将所有文本框值和所有行插入数据库中.
我使用如下代码:
Hello,
I am using the grid and create a new row dynamically when add row button click. so now how to insert this all textboxes value with all the row into the database.
i use the code like below:
<asp:GridView ID="Gridview2" runat="server" AutoGenerateColumns="False"
EmptyDataText="No records" ForeColor="Black"
onselectedindexchanged="Gridview2_SelectedIndexChanged" ShowFooter="True"
Width="100%">
<Columns>
<asp:BoundField DataField="RowNumber" HeaderText="No." >
<ItemStyle Width="2%" />
</asp:BoundField>
<asp:TemplateField HeaderText="Product Name">
<ItemTemplate>
<asp:TextBox ID="txt_product" runat="server" Columns="15"></asp:TextBox>
</ItemTemplate>
<ItemStyle Width="10%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Packing">
<ItemTemplate>
<asp:TextBox ID="txt_pack" runat="server" Text="" Columns="7"></asp:TextBox>
</ItemTemplate>
<ItemStyle Width="2%" />
</asp:TemplateField>
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="ButtonAdd" runat="server" Text="Add Product" onclick="ButtonAdd_Click" />
</FooterTemplate>
<ItemStyle Width="2%" />
</asp:TemplateField>
</Columns>
</asp:GridView>
并使用以下代码在运行时添加行:
and use the code like below to add the Row at runtime:
if (!IsPostBack)
{
SetInitialRow();
}
private void SetInitialRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
dt.Columns.Add(new DataColumn("Column1", typeof(string)));
dt.Columns.Add(new DataColumn("Column2", typeof(string)));
dr = dt.NewRow();
dr["RowNumber"] = 1;
dr["Column1"] = string.Empty;
dr["Column2"] = string.Empty;
dt.Rows.Add(dr);
//dr = dt.NewRow();
//Store the DataTable in ViewState
ViewState["CurrentTable"] = dt;
Gridview2.DataSource = dt;
Gridview2.DataBind();
}
推荐答案
请参阅本文.
使用Gridview插入,更新,删除...简单方法 [ ^ ]
Refer this article.
Insert, Update, Delete with Gridview ... Simple Way[^]
这篇关于在网格中创建动态行时如何插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文