单击删除按钮后如何从显示的Gridview中清除特定行?

时间:2019-01-28 09:34:27

标签: c# asp.net stored-procedures gridview webforms

以下是我与Gridview关联的aspx代码:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" HorizontalAlign="Center" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnRowDataBound="GridView1_RowDataBound" OnRowUpdating="GridView1_RowUpdating" OnRowDeleting="GridView1_RowDeleting"  HeaderStyle-BackColor="#00a400" HeaderStyle-ForeColor="White">
        <Columns>
         <asp:BoundField DataField="Product_No" HeaderText="Product No" />
         <asp:TemplateField HeaderText="Image">
         <ItemTemplate>
          <asp:Image ID="Image1" runat="server" Height="100px" Width="120px"
          ImageUrl='<%#"data:Image/png/jpg/jpeg/gif/bmp;base64," + Convert.ToBase64String((byte[])Eval("Product_Image")) %>' />
         </ItemTemplate>         
         </asp:TemplateField>        
         <asp:BoundField DataField="Product_Name" HeaderText="Product" />
         <asp:BoundField DataField="Barcode_No" HeaderText="Barcode" />
         <asp:BoundField DataField="Product_Category_Name" HeaderText="Category" />
         <asp:TemplateField HeaderText="Price" >
         <ItemTemplate>
          <asp:TextBox ID ="TextBox3" runat="server" Width="80px" DataField="Product_Price" Text='<%#string.Format("{0:0.00}",Eval("Product_Price"))%>'/>
          <asp:Label ID="Label4" Text="AUD" runat="server"></asp:Label>
          <asp:Button ID ="Button10" runat="server" OnClick="Price_Update_Click" ValidationGroup="UpdatePrice" CommandArgument="Button11" CommandName="Update"  Text="Update" />
          <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="TextBox3" ErrorMessage="Must be greater than 0.09 .. Cannot accept integer or character or more than 2 numbers after decimal" Operator="GreaterThan" Type="Currency" ValueToCompare="0.09" Display="Dynamic" />
          <%--<asp:RegularExpressionValidator ID="RegularExpressionValidator1"  runat="server" ErrorMessage="Numbers with only 2 digits after decimal" ControlToValidate="TextBox3" ValidationExpression="^\d{1,9}\.\d{1,2}$"></asp:RegularExpressionValidator>--%>               
          </ItemTemplate>
          </asp:TemplateField>
          <asp:TemplateField HeaderText="Quantity" >
          <ItemTemplate>
          <asp:TextBox ID ="TextBox4" runat="server" Width="60px" DataField="Product_Quantity" Text='<%#Eval("Product_Quantity")%>' />
          <asp:Button ID ="Button11" runat="server" OnClick="Quantity_Update_Click" ValidationGroup="UpdateQuantity" CommandArgument="Button12" CommandName="Update"  Text="Update" />
          <%--<asp:CompareValidator ID="CompareValidator2" runat="server" ControlToValidate="TextBox4" ErrorMessage="Must be greater than 0 .. Cannot accept decimal or character" Operator="GreaterThan" Type="Integer" ValueToCompare="0" Display="Dynamic"  />--%>
          <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Cannot accept decimal or character" ControlToValidate="TextBox4" ValidationExpression="^[0-9]*$" Display="Dynamic"></asp:RegularExpressionValidator> 
          <asp:Label ID="Label5" runat="server"></asp:Label>     
          </ItemTemplate>
          </asp:TemplateField>
          <asp:BoundField DataField="Grocery_Branch_Name" HeaderText="Branch" />
          <asp:TemplateField HeaderText="Remove Product">
          <ItemTemplate>
          <asp:Button ID="Button12" runat="server" OnClick="Delete_Click" ValidationGroup="DeleteProduct" CommandArgument="Button6" CommandName="Delete" Text="Delete" CausesValidation ="false" />
          </ItemTemplate>
          </asp:TemplateField>
          </Columns>
         </asp:GridView>

下面是与通过存储过程删除产品相关的CS代码:

 protected void Delete_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            GridViewRow row = (GridViewRow)btn.NamingContainer;
            string ProductId = row.Cells[0].Text;

            string CS;
            CS = ConfigurationManager.ConnectionStrings["Grocery_DemoConnectionString"].ConnectionString; ;
            SqlConnection con = new SqlConnection(CS);
            SqlCommand cmd = new SqlCommand("DeleteProduct", con);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;

            cmd.Parameters.AddWithValue("@ProductNo", ProductId);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox("Product has been removed from the stock");
}



 protected void GridView1_RowDeleting(object sender, EventArgs e)
        {

        }

“删除存储过程”成功运行,并且正在从“删除单击事件”的数据库表中删除其中的特定产品。该消息框还会显示“产品已从库存中删除”

但是,问题是,即使该特定产品已经从数据库中删除,Gridview仍会显示该特定产品行。

如果我手动单击“刷新”按钮,则该特定已删除的产品行将不再显示在Gridview中。

但是我想要实现的是,单击删除按钮后,该特定产品行不再显示在Gridview中,因此我不必通过单击刷新按钮来手动解决它。

如果我需要在cs代码或aspx代码中包含某些内容,则 请提及它,以便我可以进行测试。

0 个答案:

没有答案