当我点击删除按钮时,相应的行被删除。但是 gridview 中的其余行变得不可见。但是当我再次单击那些链接按钮时它显示正确 .请帮我。代码在这里:
protected void LinkButton2_Click(object sender, EventArgs e)
{
try
{ SqlConnection con = obj.getcon();
con.Open();
GridView1.Visible = true;
SqlDataAdapter adapter = new SqlDataAdapter("select e.student_id,e.student_name,e.student_nric from student_details e join student_vs_testsession_details f on e.student_id=f.student_id where f.testsession_id='" + lb_testid.Text + "' ", con);
adapter.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write(ex);
}
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string id =Convert.ToString (GridView1.DataKeys[e.RowIndex].Value);
SqlConnection con = obj.getcon();
con.Open();
SqlCommand cmd = new SqlCommand("DELETE FROM student_vs_testsession_details WHERE student_id='" + id + "'", con);
// con1.Open();
int temp = cmd.ExecuteNonQuery();
if (temp != 0)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:alert(' record deleted succesfully')", true);
}
con.Close();
GridView1.Visible = true;
GridView1.DataBind();
}
最佳答案
你在这里做的是从数据库中删除它而不是从表中删除它。(这是分配给你的网格的表)
所以在这种情况下,您需要在删除行后重新填充数据表,或者手动从绑定(bind)到 gridview 的数据表中删除行
你可以做的就是添加
LinkButton2_Click(null,null) GridView1.Visible = true; 删除函数
或重写数据集函数。
但不要忘记,每次您需要将表格分配给 gridview
希望对你有帮助
关于c# - 当一行的删除按钮被点击时整个gridview变得不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15894343/