尽管我有正确的代码,但删除按钮正在删除随机项目

Delete button is deleting random item although I have the right code

我有一个从数据库获取数据的网格视图。它还具有删除功能,并设置为根据 productID 删除一行。但是,当我尝试删除一行时,是的,它是随机删除一行。不是我要删除的。

这是我的网格视图:

<asp:GridView ID="gdview"  runat="server" AutoGenerateColumns="False"  
    DataKeyNames="ProductID"  OnRowCancelingEdit="gdview_RowCancelingEdit" 
    OnRowDeleting="gdview_RowDeleting" Width="100%" CssClass="table table-hover table-striped" OnRowDataBound="gdview_RowDataBound">
    <Columns>
        <asp:BoundField HeaderText="ID" DataField="ProductID" SortExpression="ProductID" >
            <ItemStyle Height="20px" Width="150px" />
        </asp:BoundField>

        <asp:BoundField HeaderText="Name" DataField="Name" SortExpression="Name" >
            <ItemStyle Height="20px" Width="150px" />
        </asp:BoundField>

        <asp:BoundField HeaderText="ProductCategory" DataField="CategoryName" SortExpression="CategoryNaame" >
            <ItemStyle Height="20px" Width="150px" />
        </asp:BoundField>

        <asp:BoundField HeaderText="Price" DataField="Price" SortExpression="Price" >
            <ItemStyle Height="20px" Width="150px" />
        </asp:BoundField>

        <asp:ImageField HeaderText ="ImageUrl" DataImageUrlField="ImageUrl" SortExpression="ImageUrl" ControlStyle-Width ="10">

        <ControlStyle Width="50px"></ControlStyle>

        </asp:ImageField>

        <asp:BoundField HeaderText="ProductQuantity" DataField="ProductQuantity" SortExpression="ProductQuantity" >
            <ItemStyle Height="20px" Width="150px" />
        </asp:BoundField>

        <asp:BoundField HeaderText="ProductSold" DataField="ProductSold" SortExpression="ProductSold" >
            <ItemStyle Height="20px" Width="150px" />
        </asp:BoundField>

        <asp:BoundField HeaderText="AvailableStock" DataField="AvailableStock" SortExpression="AvailableStock" >
            <ItemStyle Height="20px" Width="150px" />
        </asp:BoundField>

        <%--<asp:CommandField ShowEditButton="True">
            <ItemStyle Width="100px" />
        </asp:CommandField>--%>

        <asp:TemplateField>
        <ItemTemplate>
        <asp:LinkButton ID="lnkdel" runat="server" Text="Delete" CommandName="Delete" 
        OnClientClick="return confirm('Confirm Delete?');"></asp:LinkButton>

        </ItemTemplate>
        <ItemStyle Width="100px" />

        </asp:TemplateField>
    </Columns>

这是我的代码。具体删除功能:

protected void gdview_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

        int prodid = int.Parse(gdview.DataKeys[0].Value.ToString());
        SqlConnection conn = new SqlConnection("Data Source = 'PAULO'; Initial Catalog=ShoppingCartDB;Integrated Security =True");
        SqlDataAdapter da = new SqlDataAdapter("", conn);
        conn.Open();
        da.DeleteCommand = new SqlCommand("delete from Products where ProductID=" + prodid, conn);
        da.DeleteCommand.ExecuteNonQuery();
        conn.Close();
        GetProducts(0);
    }

更改代码

int prodid = Convert.ToInt32(gdview.DataKeys[e.RowIndex].Values["ProductID"].ToString());

来自

int prodid = int.Parse(gdview.DataKeys[0].Value.ToString());