How to delete row in gridview using rowdeleting event?

前端 未结 16 1605

This is my .cs code :

protected void Gridview1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
 Gridview1.DeleteRow(e.RowIndex);
 Gridvi         


        
16条回答
  •  清歌不尽
    2020-12-03 16:46

    Your delete code looks like this

    Gridview1.DeleteRow(e.RowIndex);
    Gridview1.DataBind();
    

    When you call Gridview1.DataBind() you will populate your gridview with the current datasource. So, it will delete all the existent rows, and it will add all the rows from CustomersSqlDataSource.

    What you need to do is delete the row from the table that CustomersSqlDataSource querying.

    You can do this very easy by setting a delete command to CustomersSqlDataSource, add a delete parameter, and then execute the delete command.

    CustomersSqlDataSource.DeleteCommand = "DELETE FROM Customer Where CustomerID=@CustomerID"; // Customer is the name of the table where you take your data from. Maybe you named it different 
    CustomersSqlDataSource.DeleteParameters.Add("CustomerID", Gridview1.DataKeys[e.RowIndex].Values["CustomerID"].ToString());
    CustomersSqlDataSource.Delete();
    Gridview1.DataBind();
    

    But take into account that this will delete the data from the database.

提交回复
热议问题