GridView多行插入操作

本文介绍如何使用 ASP.NET 中的 GridView 控件实现数据的动态增加和删除功能。通过示例代码展示了如何设置 GridView 的属性以适应动态操作,并在后台 C# 代码中实现了增加和删除行的具体逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、页面代码

                                       <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" CellPadding="2"
                                        CellSpacing="1" border="0" CssClass="gridView3" GridLines="None" OnRowCommand="GridView1_RowCommand"
                                        SkinID="11" ShowHeaderWhenEmpty="true" Width="392px">
                                        <RowStyle BackColor="#FFFFFF" Height="26px" />
                                        <EmptyDataRowStyle BackColor="#FFFFFF" Height="26px" />
                                        <Columns>
                                            <asp:TemplateField HeaderText="姓名">
                                                <ItemTemplate>
                                                    <asp:TextBox runat="server" ID="txtName" Text='<%#Eval("name") %>' Width="95%">
                                                    </asp:TextBox></ItemTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="职务">
                                                <ItemTemplate>
                                                    <asp:TextBox runat="server" ID="txtPosition" Text='<%#Eval("position") %>' Width="95%">
                                                    </asp:TextBox>
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="删除" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="40px">
                                                <ItemTemplate>
                                                    <asp:ImageButton ID="ImageButton1" runat="server" CommandArgument='<%#Container.DataItemIndex%>'
                                                        ImageUrl="~/App_Themes/Default/images/del.png" CausesValidation="false" />
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                        </Columns>
                                        <EmptyDataTemplate>
                                            <div style="background-color: white; height: 26px; line-height: 26px; text-align: center;
                                                color: Red;">
                                                无</div>
                                        </EmptyDataTemplate>
                                    </asp:GridView>
                                    <asp:Button ID="btnAddRow" runat="server" Text="增加行" OnClick="btnAddRow_Click" CausesValidation="false" />

2、后台代码

//增加行
        protected void btnAddRow_Click(object sender, EventArgs e)
        {
            DataTable dt = CreateTable();
            foreach (GridViewRow item in this.GridView1.Rows)
            {
                TextBox txtName = item.Cells[0].FindControl("txtName") as TextBox;
                TextBox txtPosition = item.Cells[0].FindControl("txtPosition") as TextBox;
                DataRow dr = dt.NewRow();
                dr["name"] = txtName.Text;
                dr["position"] = txtPosition.Text;
                dt.Rows.Add(dr);
            }
            DataRow dr2 = dt.NewRow();
            dr2["name"] = "";
            dr2["position"] = "";
            dt.Rows.Add(dr2);
            BindGridView(dt);
        }

//删除行

protected void gvAccList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "lbtnDelete")
            {
                int rowIndex = Convert.ToInt32(e.CommandArgument.ToString());
                string accStr = "";
                foreach (GridViewRow row in gvAccList.Rows)
                {
                    Label lblName = (Label)row.FindControl("lblName");
                    HtmlAnchor link1 = (HtmlAnchor)row.FindControl("link1");
                    if (lblName != null && row.RowIndex != rowIndex)
                    {
                        accStr += link1.HRef + "," + lblName.Text + "|";
                    }
                }
                lblUploadResult.Text = accStr;
                BindGvAccList(accStr);
            }
        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

奋斗---现在进行时

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值