gridview的增删改

 <asp:GridView ID="grd_Item" runat="server" AutoGenerateColumns="False" GridLines="None"
                CellPadding="10" ForeColor="#333333" PageSize="3" AllowSorting="True" OnPageIndexChanging="grd_Item_PageIndexChanging"
                AllowPaging="True" OnRowEditing="grd_Item_RowEditing" OnRowUpdating="grd_Item_RowUpdating"
                OnRowDeleting="grd_Item_RowDeleting" DataKeyNames="spec_id" OnRowCancelingEdit="grd_Item_RowCancelingEdit">
                <FooterStyle BackColor="White" ForeColor="#000066" />
                <Columns>
                    <asp:TemplateField>
                        <HeaderTemplate>
                            <asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="True" Font-Size="9pt" OnCheckedChanged="CheckBox2_CheckedChanged"
                                Text="全选" />
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:CheckBox ID="CheckBox1" runat="server" />
                            <asp:Label ID="lbl_Itemspec" runat="server" Text='<%#Eval("spec_id") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField HeaderText="Batch Code Lot NO" DataField="batch_code" />
                    <asp:BoundField HeaderText="Step Code Date Code" DataField="step_code" />
                    <asp:BoundField HeaderText="Qty" DataField="qty" />
                    <asp:BoundField HeaderText="Warranty(month)" DataField="warranty" />
                    <asp:CommandField HeaderText="update" ShowEditButton="True" />
                    <asp:TemplateField HeaderText="delete">
                        <ItemTemplate>
                            <asp:LinkButton ID="ldel" runat="server">delete</asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <RowStyle ForeColor="#000066" />
                <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
                <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
            </asp:GridView>
            <asp:Button ID="btn_del" runat="server" Text="批量删除" OnClick="btn_del_Click" />

后台:

 protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < grd_Item.Rows.Count; i++)
            {
                CheckBox chk = (CheckBox)grd_Item.Rows[i].FindControl("CheckBox1");
                CheckBox chk2 = sender as CheckBox;
                if (chk2.Checked == true)
                {
                    chk.Checked = true;
                }
                else
                {
                    chk.Checked = false;
                }
            }
        }

        protected void grd_Item_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grd_Item.PageIndex = e.NewPageIndex;
            showItem(); //重新绑定GridView数据的函数
            AppendRowForUpdate();
        }

        protected void grd_Item_RowEditing(object sender, GridViewEditEventArgs e)
        {
            grd_Item.EditIndex = e.NewEditIndex;
            showItem();
        }

        protected void grd_Item_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int spec_id = ((Label)grd_Item.Rows[e.RowIndex].FindControl("lbl_Itemspec")).Text.getInt();
            this.grd_Item.EditIndex = e.RowIndex;

            string batch_code = ((TextBox)(grd_Item.Rows[e.RowIndex].Cells[1].Controls[0])).Text;

            string step_code = ((TextBox)(grd_Item.Rows[e.RowIndex].Cells[2].Controls[0])).Text;

            int qty = ((TextBox)grd_Item.Rows[e.RowIndex].Cells[3].Controls[0]).Text.getInt();

            string warranty = ((TextBox)grd_Item.Rows[e.RowIndex].Cells[4].Controls[0]).Text;

            B_Item.updateItemSpac(batch_code, step_code, qty, warranty, spec_id);

            grd_Item.EditIndex = -1;

            showItem();
            AppendRowForUpdate();


        }

        protected void grd_Item_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int spec_id = ((Label)grd_Item.Rows[e.RowIndex].FindControl("lbl_Itemspec")).Text.getInt();
            B_Item.deleteItemSpac(spec_id);
            showItem();
            AppendRowForUpdate();

        }
        protected void grd_Item_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grd_Item.EditIndex = -1;
            showItem();
        }

        protected void btn_del_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < grd_Item.Rows.Count; i++)
            {
                CheckBox chk = (CheckBox)grd_Item.Rows[i].FindControl("CheckBox1");
                if (chk.Checked)
                {
                    int id = grd_Item.DataKeys[i].Value.getInt();
                    B_Item.deleteItemSpac(id);
                }
            }
            showItem();
        }

 补充:前台行号:<%#Container.ItemIndex %>
后台行号:int index = e.Item.ItemIndex;(在Repeater1_ItemDataBound事件中)

 

 

数据源绑定控件的Row/ItemDataBound事件

http://blog.csdn.net/sou3713/article/details/8968834?utm_source=jiancool

怎么样就统计Gridview中的一列?

https://zhidao.baidu.com/question/567904680.html

增删查改

http://www.cnblogs.com/sufei/archive/2010/03/27/1698590.html

posted @ 2017-07-07 12:37  suan1717  阅读(274)  评论(0编辑  收藏  举报