如何使用CheckBox删除GridView中的行?

时间:2014-03-17 17:51:14

标签: c# asp.net gridview checkbox delete-row

我正在做一个电子商务,我遇到了一些麻烦。

我有一个带有gridview的名为“carrinho”的会话。

我想删除购物车中检查的行。

我必须做什么?

<asp:GridView ID="GridView1" runat="server" >
    <Columns>
        <asp:TemplateField HeaderText="Produto">
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Eval("nome") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Foto">
            <ItemTemplate>
                <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("produto.foto") %>' Height="20px" Width="23px" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Quantidade">
            <ItemTemplate>
                <asp:TextBox ID="txtQuantidade" runat="server" Width="20px" Text='<%# Bind("quantidade") %>' ></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Preço">
            <ItemTemplate>
                <asp:Label ID="lblPreco" runat="server" Text='<%# Eval("produto.preco","{0:C}") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="SubTotal">
            <ItemTemplate>
                <asp:Label ID="Label4" runat="server" Text='<%# Eval("Subtotal","{0:C}") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Selecione">
            <ItemTemplate>
                <asp:CheckBox ID="CheckBox1" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<asp:Label ID="lblPrecoTotal" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Remover" Width="109px" />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Confirmar" Width="108px" />

3 个答案:

答案 0 :(得分:0)

试试这个......

protected void btndelete_Click(object sender, EventArgs e)
    {
        CheckBox cb;
        int[] idArr = new int[Gridview1.Rows.Count];
        int idcount = 0;
        foreach (GridViewRow row in Gridview1.Rows)
        {
            cb = (CheckBox)row.FindControl("chkdelete");
            if (cb != null)
            {
                if (cb.Checked)
                {

                    int ID = Convert.ToInt32(Gridview1.DataKeys[row.RowIndex].Values["ID"].ToString());
                    idArr[idcount] = ID;
                    idcount++;
                }
            }
        }

        //Bind the gridview again...
    }

这里我们要在绑定Gridview时将ID设置为datakey name。然后我们将已检查的ID收集到idArr ..然后我们可以逐个删除..

答案 1 :(得分:0)

你应该使用OnCheckedChanged事件并删除所选的gridview行试试这个http://www.codeproject.com/Questions/703052/How-to-delete-record-particular-gridview-row-check

答案 2 :(得分:-1)

 public int DeleteSKUMappingItems(string SKUTypeCode, string SellingSKUIds)
    {
        int _returnVal = -1;
        try
        {
            DbParam[] param = new DbParam[2]{
                                   new DbParam("@SKUTypeCode", SKUTypeCode, SqlDbType.VarChar), 
                                   new DbParam("@SearchString", SellingSKUIds, SqlDbType.VarChar)
                                };
            _returnVal = Db.Update("usp_DeleteSKUMappingItems", param);
        }
        catch (Exception ex)
        {
            _returnVal = -1;
             WINIT.ErrorLog.ErrorLogger.GetInstance().Log(ex, WINIT.ErrorLog.LogSeverity.Error);
        }
        return _returnVal;
    }