For Answers, see/post comments

Deletion in Grid view

How to Delete a selected row in gridview using asp.net 2005 ?

3 comments:

Anonymous said...

protected void gvSample_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int i = e.RowIndex;

DataTable dt = (DataTable)Session["dt"];
dt.Rows[i].Delete();
dt.AcceptChanges();

gvSample.DataSource = dt.DefaultView;
gvSample.DataBind();
}

Anonymous said...

Hi ,
you can get the id of the record which the user clicked to delete by using the following (If u are binding the id to the first column, whether u make it visible or not)

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int id = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[0].Text);
// use SQL Command or stored procedure or what ever way u follow to delte the record from the database.

}

Anonymous said...

Hi,

Try this
protected void Delete_Click (object sender, EventArgs e)
{

GridViewRow r = grid1.SelectedRow;
string id = r.Cells[0];
SqlConnection sql=new SqlConnection(.......);
SqlCommand cmd=new SqlCommand("Delete from table where id="+id);
}