删除GridView链接按钮

时间:2018-12-18 09:46:18

标签: c# asp.net

当用户单击“删除链接”按钮时,我正在尝试删除gridview中的行,代码正常工作,但是在用户界面中,gridview完全消失了。如何修复消失的gridview? 我只希望实际删除的行消失而不是整个gridview消失吗?

这是我的代码

  void PopulateGridview()
{
    DataTable dtble = new DataTable();
    using (SqlConnection con = new SqlConnection(conString))
    {
        con.Open();
        SqlDataAdapter adapt = new SqlDataAdapter("select v.visitor_Fname,v.visitor_Mname,v.visitor_family_name,v.visitor_mobile,v.visitor_table_id,v.place_of_work,v.country_name from visitor v join requests r on r.request_id =v.request_id where  r.request_id =(select MAX(request_id) from requests where user_id='" + (int)Session["empNo"] + "' )", con);
        adapt.Fill(dtble);
    }
    if (dtble.Rows.Count > 0)
    {
        GridView2.DataSource = dtble;
        GridView2.DataBind();
    }
    else
    {
        dtble.Rows.Add(dtble.NewRow());
        GridView2.DataSource = dtble;
        GridView2.DataBind();
        GridView2.Rows[0].Cells.Clear();
        GridView2.Rows[0].Cells.Add(new TableCell());
        GridView2.Rows[0].Cells[0].ColumnSpan = dtble.Columns.Count;
        GridView2.Rows[0].Cells[0].Text = " بيانات الزائر";
        GridView2.Rows[0].Cells[0].HorizontalAlign = HorizontalAlign.Center;


    }

     protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName.Equals("Add"))
    {
        SqlConnection con2 = new SqlConnection(conString);



                con2.Open();

                    String a = "Insert into dbo.visitor(visitor_Fname,visitor_Mname,visitor_family_name,visitor_mobile,place_of_work,country_name, request_id)values(@FName,@MName,@FamilyName,@Mobile,@work,@country,'" + Last_request_id.ToString() + "')";

                    SqlCommand cmd2 = new SqlCommand(a, con2);

                    cmd2.Parameters.AddWithValue("@FName", (GridView2.FooterRow.FindControl("FirstNameFooter") as TextBox).Text.Trim());
                    cmd2.Parameters.AddWithValue("@MName", (GridView2.FooterRow.FindControl("MNameFooter") as TextBox).Text.Trim());
                    cmd2.Parameters.AddWithValue("@FamilyName", (GridView2.FooterRow.FindControl("FamilyNameFooter") as TextBox).Text.Trim());
                    cmd2.Parameters.AddWithValue("@Mobile", (GridView2.FooterRow.FindControl("visitorMobileFooter") as TextBox).Text.Trim());
                    cmd2.Parameters.AddWithValue("@work", (GridView2.FooterRow.FindControl("place_of_workFooter") as TextBox).Text.Trim());
                    cmd2.Parameters.AddWithValue("@country", (GridView2.FooterRow.FindControl("country") as DropDownList).SelectedValue.Trim());

                    cmd2.ExecuteNonQuery();
                    PopulateGridview();

           }


            con2.Close();

    }

  protected void LinkButton2_Click(object sender, EventArgs e)
{
    SqlConnection con2 = new SqlConnection(conString);
    con2.Open();


        LinkButton butt = (LinkButton)sender;
        int ID = Convert.ToInt32(butt.CommandArgument.ToString());
        string del = "delete from visitor where visitor_table_id =@ID";

        SqlCommand cmd2 = new SqlCommand(del, con2);


        cmd2.Parameters.AddWithValue("@ID", ID);
        cmd2.ExecuteNonQuery();

        GridView2.DataBind();

    con2.Close();

}

2 个答案:

答案 0 :(得分:1)

我认为您应该在执行delete命令之后尝试重新填充DataGrid的{​​{1}}属性。含义,请尝试交换:

DataSource in GridView2.DataBind();中的呼叫:

LinkButton2_Click

希望有帮助!

答案 1 :(得分:0)

您必须再次在点击事件中加载DataSource。比致电DataBind()