当我点击删除按钮时,相应的行将被删除。但gridview中的剩余行变为invisibe。但是当我再次单击这些链接按钮时它会正确显示 。请帮我。代码在这里:
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();
}
答案 0 :(得分:0)
缺少网格视图的数据源
Gridview1.DataSource = dt;
添加此项作为重新绑定它所需的数据源。
答案 1 :(得分:0)
你在这里做的是从数据库中删除它,而不是从表中删除它。(这是分配给你网格的表格)
因此,在这种情况下,您需要在删除行后重新填充数据表或手动删除绑定到gridview的数据表中的行
你可以做的是添加
删除功能 GridView1.Visible = true; 后LinkButton2_Click(null,null)
或重写数据集功能。
但是不要忘记,每次你需要将你的表分配给gridview
希望这有帮助