Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
how to delete the row from gridview in asp.net c#.
========================================

Dear Frnds,

Am displaying data in gridview by using sql datasource. and also from left side of gridview i have edit and delete links manually selected from grid properties.

Records are Deleting Perfectly.......But i need Confirmation Message using Javascript...that RECORDS Deleted successfully


Please help me this problem,

Thanks.
Posted

Please refer:
How to implement row deletion in GridView, with confirmation.
GridView Delete, with Confirmation[^]

Updating, Deleting records using GridView control[^]
 
Share this answer
 
v2
Delete.aspx

ASP.NET
<div>
        <asp:gridview id="GridView1" runat="server" onrowdeleting="GridView1_RowDeleting" xmlns:asp="#unknown">
            OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit"
            Style="margin-bottom: 0px" AutoGenerateColumns="False" ForeColor="#CC0099" GridLines="Vertical">
            <columns>
                <asp:templatefield headertext="Username">
                    <itemtemplate>
                        <asp:label id="lblName" runat="server" text="<%#Bind("username")%>"></asp:label>
                    </itemtemplate>
                    <edititemtemplate>
                        <asp:textbox id="txtusername" runat="server" text="<%#Bind("username")%>"></asp:textbox>
                    </edititemtemplate>
                </asp:templatefield>
                <asp:templatefield headertext="File Type">
                    <itemtemplate>
                        <asp:label id="lblfiletype" runat="server" text="<%#Bind("filetype")%>"></asp:label>
                    </itemtemplate>
                    <edititemtemplate>
                        <asp:textbox id="txtfiletype" runat="server" text="<%#Bind("filetype")%>"></asp:textbox>
                    </edititemtemplate>
                </asp:templatefield>
                <asp:templatefield headertext="File Path">
                    <itemtemplate>
                        <asp:label id="lblfilepath" runat="server" text="<%#Bind("filepath")%>"></asp:label>
                    </itemtemplate>
                    <edititemtemplate>
                        <asp:textbox id="txtfilepath" runat="server" text="<%#Bind("filepath")%>">
                        </asp:textbox>
                    </edititemtemplate>
                </asp:templatefield>
                <asp:templatefield headertext="File Name">
                    <itemtemplate>
                        <asp:label id="lblfilename_upload" runat="server" text="<%#Bind("filename_upload")%>"></asp:label>
                    </itemtemplate>
                    <edititemtemplate>
                        <asp:textbox id="txtfilename_upload" runat="server" text="<%#Bind("filename_upload")%>">
                        </asp:textbox>
                    </edititemtemplate>
                </asp:templatefield>
                <asp:templatefield headertext="Creation Date">
                    <itemtemplate>
                        <asp:label id="lblcreatedon" runat="server" text="<%#Bind("createdon")%>"></asp:label>
                    </itemtemplate>
                    <edititemtemplate>
                        <asp:textbox id="txtcreatedon" runat="server" text="<%#Bind("createdon")%>">
                        </asp:textbox>
                    </edititemtemplate>
                </asp:templatefield>
                <asp:templatefield headertext="File Size">
                    <itemtemplate>
                        <asp:label id="lblsize" runat="server" text="<%#Bind("size")%>"></asp:label>
                    </itemtemplate>
                    <edititemtemplate>
                        <asp:textbox id="txtsize" runat="server" text="<%#Bind("size")%>">
                        </asp:textbox>
                    </edititemtemplate>
                </asp:templatefield>
                <asp:templatefield headertext="ID">
                    <itemtemplate>
                        <asp:label id="lblProductid" runat="server" visible="true" text="<%#Bind("ID")%>"></asp:label>
                    </itemtemplate>
                    <edititemtemplate>
                        <asp:label id="lblProductid" runat="server" visible="false" text="<%#Bind("ID")%>">
                        </asp:label>
                    </edititemtemplate>
                </asp:templatefield>
                <asp:templatefield itemstyle-width="50px" headertext="Edit">
                    <itemtemplate>
                        <asp:linkbutton id="lbtnedit" runat="server" forecolor="#0C4375" text="Edit" commandname="Edit"></asp:linkbutton>
                    </itemtemplate>
                    <edititemtemplate>
                        <asp:linkbutton id="lbtnupdate" runat="server" forecolor="#0C4375" text="Update">
                            CommandName="Update"></asp:linkbutton>
                        <asp:linkbutton id="lbtncancel" runat="server" forecolor="#0C4375" text="Cancel">
                            CommandName="Cancel"></asp:linkbutton>
                    </edititemtemplate>
                    <itemstyle width="50px"></itemstyle>
                </asp:templatefield>
                <asp:templatefield itemstyle-width="50px" headertext="Delete">
                    <itemtemplate>
                        <asp:linkbutton id="lbtnDelete" runat="server" forecolor="#0C4375" text="Delete">
                            CommandName="Delete"></asp:linkbutton>
                    </itemtemplate>
                    <itemstyle horizontalalign="Left" wrap="True" />
                    <controlstyle backcolor="#EAF6FE" />
                </asp:templatefield>
            </columns>
            <footerstyle forecolor="#CC66FF" />
            <HeaderStyle Font-Italic="True" ForeColor="#FF3300" />
        </asp:gridview>
        <table>
        </table>
    </div>


delete.cs:


C#
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
   {
       Label lblProductid = GridView1.Rows[e.RowIndex].FindControl("lblProductid") as Label;
       int prodcutid = Convert.ToInt32(lblProductid.Text);
       query = "delete from attachment where ID= " + prodcutid + " ";
       setConnectionString();
       Message = EmpBal.binddeleteuserBAL(query, conString);
       if (Message == "success")
       {
           Bind();
       }

   }

}
 
Share this answer
 
Make a button as template field
C#
<asp:templatefield xmlns:asp="#unknown">
                 <itemtemplate>
                 <asp:linkbutton id="lb1" runat="server" causesvalidation="false" onclientclick="return <b>confirm('Are You sure to delete this entry')</b>" text="Delete">CommandArgument='<%#Eval("id")%>' ></asp:linkbutton>
                 </itemtemplate>
                 
                 </asp:templatefield>


and on row command event of gridview use as
C#
  protected void rowcmd(object sender, GridViewCommandEventArgs e)
    {
    
        try
        {
           
            Int32 id = Convert.ToInt32(e.CommandArgument);
            string comm = "Delete from tblComments where id=@id";
            SqlCommand cmd = new SqlCommand(comm, Db.GetConnection());
            cmd.Parameters.AddWithValue("id", id);
            cmd.ExecuteNonQuery();
             GridView1.DataBind();
        }
        catch (Exception ex)
        {
        }

    }
}
 
Share this answer
 
v2

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