如何使用 C# ASP.NET 通过按钮复制(克隆)Datagridview 中的现有行
我有一个 DataGridView,其中有一列带有按钮。
现在,我希望每次单击按钮时,都会复制 DataGridView 中的选定行并将其直接插入到选定行之后。
我想复制所有列的所有值并将这个新行插入到我的数据库表中。
我怎样才能意识到这一点?
非常感谢。
在我下面的代码中,错误位于这一行
cgv.Rows.Add(gvRow.Cells[1].Text);
<asp:GridView runat="server" ID="gvCustomers" AutoGenerateColumns="false"
OnSelectedIndexChanged="OnSelectedIndexChanged">
<Columns>
<asp:TemplateField ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:ImageButton ID="btselect" runat="server"
CommandName="Select"
OnClick="Copy"
ImageUrl="/aspnet/img/clone_icon.gif"
ToolTip="Clone row" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CustomerId" HeaderText="ID" />
</Columns>
</asp:GridView>
protected void Copy(object sender, EventArgs e)
{
GridViewRow gvRow = this.gvProducts.SelectedRow;
DataTable cgv = null;
if (ViewState["Customers"] != null)
{
cgv = ViewState["Customers"] as DataTable;
cgv.Rows.Add(gvRow.Cells[1].Text);
ViewState["Customers"] = cgv;
}
BindData();
}
protected void gvProducts_SelectedIndexChanged(object sender, EventArgs e)
{
this.gvProducts.SelectedRow.BackColor = System.Drawing.Color.Cyan;
}
I have a DataGridView with a column which with button.
Now I want that with every click on the button the selected row in the DataGridView is copied and inserted directly after the selected row.
I want to duplicate all value of all columns and insert into table of my database this new row.
How can I realize this ?
Many thanks in advance.
On my code below the error is on this line
cgv.Rows.Add(gvRow.Cells[1].Text);
<asp:GridView runat="server" ID="gvCustomers" AutoGenerateColumns="false"
OnSelectedIndexChanged="OnSelectedIndexChanged">
<Columns>
<asp:TemplateField ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:ImageButton ID="btselect" runat="server"
CommandName="Select"
OnClick="Copy"
ImageUrl="/aspnet/img/clone_icon.gif"
ToolTip="Clone row" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CustomerId" HeaderText="ID" />
</Columns>
</asp:GridView>
protected void Copy(object sender, EventArgs e)
{
GridViewRow gvRow = this.gvProducts.SelectedRow;
DataTable cgv = null;
if (ViewState["Customers"] != null)
{
cgv = ViewState["Customers"] as DataTable;
cgv.Rows.Add(gvRow.Cells[1].Text);
ViewState["Customers"] = cgv;
}
BindData();
}
protected void gvProducts_SelectedIndexChanged(object sender, EventArgs e)
{
this.gvProducts.SelectedRow.BackColor = System.Drawing.Color.Cyan;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,您处于一个很好的位置,您尝试将数据行添加到数据源或您在会话中保留的数据表。
所以,我会这样做:
请记住,GV 中的任何模板字段都需要使用查找控件,但从您发布的内容来看,使用 .cells[] 集合确实应该适合您。
Ok, you are in a good place in that you attempting to add the data row to the data source, or the datatable that you are persiting in session.
So, I would do it this way:
Do remember that any template field in the GV needs to use find control, but from what you posted, it does look like using .cells[] collection should work for you.