asp:GridView 使用增加删除行

 <asp:GridView ID="gv_Office" runat="server" AutoGenerateColumns="false" GridLines="None" OnRowDataBound="gv_Office_RowDataBound" HeaderStyle-CssClass="headercss" Width="100%">
                    <Columns>
                        <asp:TemplateField ItemStyle-Width="50px" HeaderStyle-HorizontalAlign="Left" HeaderStyle-CssClass="xhcss" ItemStyle-CssClass="xhcss">
                            <HeaderTemplate>
                                <b><%= Resources.Resource.lbl_NO%></b>
                            </HeaderTemplate>
                            <ItemTemplate>
                                <%# string.Format("{0}", Container.DataItemIndex+1)%>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField ItemStyle-CssClass="ls">
                            <HeaderTemplate>
                                <b><%= Resources.Resource.lbl_CommodityMoveInfo%></b>
                            </HeaderTemplate>
                            <ItemTemplate>
                                <table style="width: 100%;" border="0" id="detailed">
                                    <tr>
                                        <td rowspan="3" style="height: 125px; width: 280px;">
                                            <div class="imgbox">
                                            <asp:Image ID="imgPhoto" runat="server" ImageUrl='<%# Eval("Photo")%>'  alt="" onclick="OpenImage(this);" />
                                            </div>
                                            <asp:Button ID="btnUploadImg" runat="server" Text="上传图片" tag="photo" CssClass="UpLoadImage" />
                                            <asp:HiddenField ID="hdPhoto" runat="server" Value='<%# Eval("Photo")%>' />
                                        </td>
                                        <td style="text-align: right;"><%= Resources.Resource.lbl_CommodityName%>:<font
                                            color="red">*</font></td>
                                        <td style="width: 160px;">
                                            <asp:TextBox ID="tb_CommodityName" runat="server" Text='<%# Eval("CommodityName") %>' tag="string"></asp:TextBox>
                                            <asp:DropDownList ID="ddl_CommodityName" runat="server" Width="150px" Visible="false" AutoPostBack="true" OnSelectedIndexChanged="ddl_CommodityName_SelectedIndexChanged" tag="ddl"></asp:DropDownList>
                                            <asp:HiddenField ID="hf_CommodityID" runat="server" Value='<%# Eval("CommodityID") %>' />
                                        </td>
                                        <td style="text-align: right; width: 80px;"><%= Resources.Resource.lbl_Unit%>:<font
                                            color="red">*</font></td>
                                        <td style="width: 160px;">
                                            <asp:TextBox ID="tb_Unit" runat="server" Text='<%# Eval("Unit") %>' tag="string"></asp:TextBox>
                                        </td>
                                        <td style="text-align: right; width: 80px;"><%= Resources.Resource.lab_Quantity%>:<font
                                            color="red">*</font></td>
                                        <td style="width: 160px;">
                                            <asp:TextBox ID="tb_Count" runat="server" Text='<%# Eval("Count") %>' tag="number"></asp:TextBox>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td style="text-align: right;"><%= Resources.Resource.lbl_MoveOutReason%>:<font
                                            color="red">*</font></td>
                                        <td colspan="3">
                                            <asp:TextBox ID="tb_MoveOutReason" runat="server" Text='<%# Eval("MoveOutReason") %>' Width="390" tag="string"></asp:TextBox>
                                        </td>
                                        <td style="text-align: right;">
                                            <asp:Label ID="l_ReturnDate" runat="server"><%= Resources.Resource.lbl_ReturnDate%>:<font
                                            color="red">*</font></asp:Label></td>
                                        <td>
                                            <asp:TextBox ID="tb_ReturnDate" runat="server" CssClass="Wdate" onFocus="LoseCursor(this);WdatePicker({dateFmt:'yyyy-MM-dd',lang:''+GetLanguang()+''});" Text='<%# Eval("ReturnDate")!=""? Convert.ToDateTime(Eval("ReturnDate")).ToString ("yyyy-MM-dd")=="1900-01-01"?"":Convert.ToDateTime(Eval("ReturnDate")).ToString ("yyyy-MM-dd"):"" %>' tag="string-date"></asp:TextBox>
                                        </td>
                                    </tr>
                                    <tr style="height: 43px;">
                                        <td style="text-align: right;">
                                            <asp:Label ID="l_lotID" runat="server" Visible="false">Lot-ID:&nbsp;&nbsp;</asp:Label></td>
                                        <td colspan="5">
                                            <asp:TextBox ID="tb_LotID" runat="server" Visible="false" Text='<%# Eval("LotID") %>'></asp:TextBox>
                                        </td>
                                    </tr>
                                </table>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="<%$ Resources:Resource, lbl_Operate %>" HeaderStyle-Width="43" HeaderStyle-HorizontalAlign="Left">
                            <ItemTemplate>
                                <asp:LinkButton ID="LinkButtonDelete" runat="server" CausesValidation="False" CommandName='<%# string.Format("{0}", Container.DataItemIndex+1) %>'
                                    OnClientClick='<%# "return confirm(\"" + Resources.Resource.validated_ConfirnDel+ "\");" %>'
                                    CssClass="icon_del" OnClick="LinkButtonDelete_Click1"></asp:LinkButton>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
                <br />
                <div style="text-align: right; margin-right: 10px;">
                    <asp:Button runat="server" ID="b_Add" CssClass="formBtn" Text="<%$ Resources:Resource, lbl_Add2 %>"
                        OnClick="b_Add_Click" />
                </div>

使用添加按钮
protected void b_Add_Click(object sender, EventArgs e)
      {
        OnRowCreating();
    }
       private void OnRowCreating()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("CommodityName");
        dt.Columns.Add("Unit");
        dt.Columns.Add("Count");
        dt.Columns.Add("MoveOutReason");
        dt.Columns.Add("ReturnDate");
        dt.Columns.Add("LotID");
        dt.Columns.Add("Photo");
        dt.Columns.Add("CommodityID");
        for (int i = 0; i < gv_Office.Rows.Count; i++)
        {
            DataRow dr = dt.NewRow();
            dr[0] = (gv_Office.Rows[i].FindControl("tb_CommodityName") as TextBox).Text.Replace("&nbsp;", "");
            dr[1] = (gv_Office.Rows[i].FindControl("tb_Unit") as TextBox).Text.Replace("&nbsp;", "");
            dr[2] = (gv_Office.Rows[i].FindControl("tb_Count") as TextBox).Text.Replace("&nbsp;", "");
            dr[3] = (gv_Office.Rows[i].FindControl("tb_MoveOutReason") as TextBox).Text.Replace("&nbsp;", "");
            dr[4] = (gv_Office.Rows[i].FindControl("tb_ReturnDate") as TextBox).Text.Replace("&nbsp;", "");
            dr[5] = (gv_Office.Rows[i].FindControl("tb_LotID") as TextBox).Text.Replace("&nbsp;", "");
            dr[6] = (gv_Office.Rows[i].FindControl("hdPhoto") as HiddenField).Value;
            dr[7] = (gv_Office.Rows[i].FindControl("hf_CommodityID") as HiddenField).Value;
            dt.Rows.Add(dr);
        }
        DataRow drNew = dt.NewRow();
        drNew[0] = "";
        drNew[1] = "";
        drNew[2] = "";
        drNew[3] = "";
        drNew[4] = "";
        drNew[5] = "";
        drNew[6] = "";
        drNew[7] = "-1";
        dt.Rows.Add(drNew);
        gv_Office.DataSource = dt;
        gv_Office.DataBind();
    }
   使用删除木一行
    protected void LinkButtonDelete_Click1(object sender, EventArgs e)
    {
        LinkButton lb = (LinkButton)sender;
        OnRowDeleting(Convert.ToInt32(lb.CommandName) - 1);
      ScriptManager.RegisterClientScriptBlock(UpdatePanel1, GetType(), "aa", "autoHover();init();", true);
    }
     private void OnRowDeleting(int index)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("CommodityName");
        dt.Columns.Add("Unit");
        dt.Columns.Add("Count");
        dt.Columns.Add("MoveOutReason");
        dt.Columns.Add("ReturnDate");
        dt.Columns.Add("LotID");
        dt.Columns.Add("Photo");
        dt.Columns.Add("CommodityID");
        for (int i = 0; i < gv_Office.Rows.Count; i++)
        {
            DataRow dr = dt.NewRow();
            dr[0] = (gv_Office.Rows[i].FindControl("tb_CommodityName") as TextBox).Text.Replace("&nbsp;", "");
            dr[1] = (gv_Office.Rows[i].FindControl("tb_Unit") as TextBox).Text.Replace("&nbsp;", "");
            dr[2] = (gv_Office.Rows[i].FindControl("tb_Count") as TextBox).Text.Replace("&nbsp;", "");
            dr[3] = (gv_Office.Rows[i].FindControl("tb_MoveOutReason") as TextBox).Text.Replace("&nbsp;", "");
            dr[4] = (gv_Office.Rows[i].FindControl("tb_ReturnDate") as TextBox).Text.Replace("&nbsp;", "");
            dr[5] = (gv_Office.Rows[i].FindControl("tb_LotID") as TextBox).Text.Replace("&nbsp;", "");
            dr[6] = (gv_Office.Rows[i].FindControl("hdPhoto") as HiddenField).Value;
            dr[7] = (gv_Office.Rows[i].FindControl("hf_CommodityID") as HiddenField).Value;
            if (i < index)
            {
                dt.Rows.Add(dr);
            }
            else if (i > index)
            {
                dt.Rows.Add(dr);
            }
        }
        gv_Office.DataSource = dt;
        gv_Office.DataBind();
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值