GridView多行插入操作

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);
            }
        }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章