Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用复选框删除GridView中的行?_C#_Asp.net_Gridview_Checkbox_Delete Row - Fatal编程技术网

C# 如何使用复选框删除GridView中的行?

C# 如何使用复选框删除GridView中的行?,c#,asp.net,gridview,checkbox,delete-row,C#,Asp.net,Gridview,Checkbox,Delete Row,我在做电子商务,我遇到了一些麻烦 我有一个cart,它是一个名为“carrinho”的会话,带有gridview 我想从购物车中删除选中的行 我该怎么办 <asp:GridView ID="GridView1" runat="server" > <Columns> <asp:TemplateField HeaderText="Produto"> <ItemTemplate>

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

我有一个cart,它是一个名为“carrinho”的会话,带有gridview

我想从购物车中删除选中的行

我该怎么办

<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" />

试试这个

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
。然后我们可以逐个删除…

您可以使用OnCheckedChanged事件并删除选定的Gridview行尝试此操作

获取选中行的ID并将其从数据中删除存储并重新绑定网格。
 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;
    }