直接从gridview中删除GridView删除的行数据

时间:2015-10-30 09:52:54

标签: c# asp.net gridview

这是删除行的代码:

    else if (e.CommandName == "Deleterow")
                {
                    GridViewRow gr = (GridViewRow)
((Button)e.CommandSource).NamingContainer;      
                    SqlCommand com = new SqlCommand("StoredProcedure4", con);
                    com.CommandType = CommandType.StoredProcedure;
                    com.Parameters.AddWithValue("@ID", gr.Cells[0].Text);
                    com.ExecuteNonQuery();


                }

ASPX:

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        SqlConnection con = Connection.DBconnection();
        if (e.CommandName == "EditRow")
        {
            GridViewRow gr = (GridViewRow)((Button)e.CommandSource).NamingContainer;               
            Textid.Text = gr.Cells[0].Text;
            Textusername.Text = gr.Cells[1].Text;
            Textclass.Text = gr.Cells[2].Text;
            Textsection.Text = gr.Cells[3].Text;
            Textaddress.Text = gr.Cells[4].Text;
        }
        else if (e.CommandName == "Deleterow")
        {
            GridViewRow gr = (GridViewRow)((Button)e.CommandSource).NamingContainer;      
            SqlCommand com = new SqlCommand("StoredProcedure4", con);
            com.CommandType = CommandType.StoredProcedure;
            com.Parameters.AddWithValue("@ID", gr.Cells[0].Text);
            com.ExecuteNonQuery();

        }
    }

当我删除按钮时,它在重新加载页面后删除了数据行。

所以我需要直接从gridview中删除该行。

为了做什么,有人可以帮忙吗?

谢谢,

1 个答案:

答案 0 :(得分:0)

            else if (e.CommandName == "Deleterow")
            {
                GridViewRow gr = (GridViewRow)
                ((Button)e.CommandSource).NamingContainer;      
                SqlCommand com = new SqlCommand("StoredProcedure4", con);
                com.CommandType = CommandType.StoredProcedure;
                com.Parameters.AddWithValue("@ID", gr.Cells[0].Text);
                com.ExecuteNonQuery();
                //Response.Redirect("studententry.aspx"); Remove this line
                //call method here you have used to populate the gridview at page load.  
            }