Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

delete from gridview rows:

code:

C#
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("delete from savetable where productid=@productid", con);
                cmd.Parameters.AddWithValue("@productid", Session["ID"].ToString());
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                GridView1.DataSource = ds;
                GridView1.DataBind();
                con.Close();
            }
        }
        catch
        {
        }      
    }    


here row not deleted, what mistake here please reply me
Posted
Updated 4-Mar-14 19:16pm
v2
Comments
Ramug10 5-Mar-14 0:56am    
you need to write delete code in grid view row command event.
member1431 5-Mar-14 1:04am    
yes
Tom Marvolo Riddle 5-Mar-14 1:23am    
solution posted .Try it and let me know
Tom Marvolo Riddle 5-Mar-14 0:58am    
get the ID and pass it
member1431 5-Mar-14 1:26am    
row not deleted and am trying

1 solution

Try this:

Markup:
ASP.NET
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
     AutoGenerateColumns="False" CellPadding="3"
     onpageindexchanging="GridView1_PageIndexChanging"
     onrowcommand="GridView1_RowCommand" onrowdeleting="GridView1_RowDeleting">
     <RowStyle ForeColor="#000066" />
     <Columns>

     <asp:TemplateField HeaderText="product id" Visible="False">
         <ItemTemplate>
             <asp:Label ID="label1" runat="server" Text='<%#Eval("productid")%>'></asp:Label>
         </ItemTemplate>
         </asp:TemplateField>

<asp:TemplateField HeaderText="Delete">
         <ItemTemplate>
         <asp:ImageButton ID="ImageButton1" runat="server"
          ImageUrl ="../image/delete.png" CommandName ="Delete" />
         </ItemTemplate>
         </asp:TemplateField>

       </Columns>
     <FooterStyle BackColor="White" ForeColor="#000066" />
     <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
     <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
     <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White"
         Height="50px" />
 </asp:GridView>


In RowCommand Event:
C#
 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
     
 if (e.CommandName == "Delete")
            {
                ImageButton img = (ImageButton)e.CommandSource as ImageButton;
                GridViewRow row = img.NamingContainer as GridViewRow;

                Label lbid = (Label)row.FindControl("label1");
                int productid = Convert.ToInt32(lbid.Text);


                 // pass the product id and perform delete process

            }
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900