如何使用 C# ASP.NET 通过按钮复制(克隆)Datagridview 中的现有行

发布于 2025-01-10 04:25:35 字数 1426 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃颗糖壮壮胆 2025-01-17 04:25:35

好的,您处于一个很好的位置,您尝试将数据行添加到数据源或您在会话中保留的数据表。

所以,我会这样做:

protected void Copy(object sender, EventArgs e)
{
    GridViewRow gvRow = this.gvProducts.SelectedRow;
    DataTable cgv = null;
    if (ViewState["Customers"] != null)
    {
        cgv = ViewState["Customers"] as DataTable;
        DataRow MyNewRow = cgv.NewRow()
        MyNewRow["FirstName"] = gvRow.Cells[2].Text;
        MyNewRow["LastName"] = gvRow.Cells[3].Text;
        MyNewRow["CustomerID"] = gvRow.Cells[0];
        cgv.Rows.Add(MyNewRow);

        ViewState["Customers"] = cgv;
    }

    BindData();
}

请记住,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:

protected void Copy(object sender, EventArgs e)
{
    GridViewRow gvRow = this.gvProducts.SelectedRow;
    DataTable cgv = null;
    if (ViewState["Customers"] != null)
    {
        cgv = ViewState["Customers"] as DataTable;
        DataRow MyNewRow = cgv.NewRow()
        MyNewRow["FirstName"] = gvRow.Cells[2].Text;
        MyNewRow["LastName"] = gvRow.Cells[3].Text;
        MyNewRow["CustomerID"] = gvRow.Cells[0];
        cgv.Rows.Add(MyNewRow);

        ViewState["Customers"] = cgv;
    }

    BindData();
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文