when you create a grid view then you will already create delete button then you can delete that data or insert data.by query fire.
To delete a row from gridview ,first enable delete button true. write sql query delete from tablename where columnvalue=@columnname in sqldatasource.
if you want to delete data from grid view than you just need to plug a delete link and need to insert a delete query than it can easily deleted from you table.
i have done my six month training in asp.net...now i am working on a live project and i am unable to delete data from grid view...please can any suggest me.
This does the trick for me : in the gridview tag , under columns , add : <asp:gridview onRowDataBound="addConfirm" DataSourceID="messages"> <columns> // Other columns with stuff <asp:templatefield AccessibleHeaderText="Delete"> <itemtemplate> <asp:linkbutton runat="server" id="rowToDelete" commandname="Delete" Text="Delete" /> </itemtemplate> </asp:templatefield> </columns> </asp:gridview> and add SQLdatasource <asp:SqlDataSource id="messages" runat="server" ConnectionString="<%$ ConnectionStrings:AkshayConnectionString %>" SelectCommand="blah blah blah" DeleteCommand="DELETE FROM table_name WHERE id=@id"> <DeleteParameters> <asparameter Name="id" /> </DeleteParameters> and in the .cs file , i use this function : protected void addConfirm(Object src, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { DataRowView rowView = (DataRowView)e.Row.DataItem; string name = rowView["title"].ToString().Trim(); LinkButton linkButton = (LinkButton)e.Row.FindControl("rowToDelete"); linkButton.Attributes["onClick"] = "javascript:return confirm ( 'Do you wish to delete " + name + " ?' )"; } }
protected void grdview1_RowDeleting(object sender, GridViewDeleteEventArgs e) { int catid = Convert.ToInt32(grdview1.DataKeys[e.RowIndex].Value); conn.Open(); SqlCommand cmd = new SqlCommand("delete emp where rowid=" + catid+ "", conn); cmd.ExecuteNonQuery(); conn.Close(); bind(); }
Hi, check this link http://www.aspdotnetcodes.com/GridView_Insert_Edit_Update_Delete.aspx It explains about editing and deleting records in gridview.