简体   繁体   中英

Deleting a row when the user presses delete button

This might be a easy problem but i don't see what i'm doing wrong here. The code is:

 protected void dgvSelected_RowCommand (object sender, GridViewCommandEventArgs e)
    {
        if(e.CommandName =="Delete")
        {
            if (!string.IsNullOrEmpty(e.CommandArgument.ToString()))
            {
                int RowIndex = Convert.ToInt32(e.CommandArgument);
                dgvSelected.DeleteRow(RowIndex);


                dgvSelected.DataBind();
            }


        }
    }

And at the end of the class i have this method.

protected void dgvSelected_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }

I'm using asp .net and i have a gridview that several rows and at each row i have a delete button where the user can press it to delete the row. For the moment when i press the delete button the whole gridview disappears. What am i doing wrong?

Try this

protected void dgvSelected_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    dgvSelected.DataBind();
}

Since the gridview data was from antoher gridview, the solution was to make a viewstate variable and save the datatable in it. And then to delete one row:

protected void dgvSelected_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int index = Convert.ToInt32(e.RowIndex);
        DataTable dt = ViewState["SelectedValue"] as DataTable;
        dt.Rows[index].Delete();
        ViewState["SelectedValue"] = dt;
        dgvSelected.DataSource = dt;
        dgvSelected.DataBind();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM