【发布时间】:2016-01-30 16:58:21
【问题描述】:
这是删除行的代码:
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 中删除该行。
为此该怎么办,有人可以帮忙吗?
谢谢,
【问题讨论】:
-
无需重新加载页面,只需在 com.ExecuteNonQuery(); 之后重新绑定您的网格即可;
-
不要使用
Response.Redirect()重新绑定你的gridview -
@Rani:你的 Page_Load 内容。这将更容易让我们为您提供适当的解决方案。
-
好的,只需分享您填充网格视图的部分,例如
GridView1.Datasource正在被调用。 -
可以
GridView1.DeleteRow(gr.Cells[0].Text);解决您的问题。stackoverflow.com/questions/11473226/…