yara
yara

Reputation: 1

delete gridview linkedbutton

I'm trying to delete the row in gridview when the user is click delete linked button, the code is work correctly but in user interface the gridview is disappearing completely. How can I fix the disappearing gridview ? I want only the row that actually deleted is disappear not the whole gridview ?

here is my code

  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();

}

Upvotes: 0

Views: 28

Answers (2)

Itay Podhajcer
Itay Podhajcer

Reputation: 2656

I think you should try to repopulated the DataGrid's DataSource property again, after you execute the delete command. meaning, try swapping:

GridView2.DataBind(); in LinkButton2_Click to a call to:

PopulateGridview()

Hope it helps!

Upvotes: 1

André
André

Reputation: 343

You have to load your DataSource in click event again. Than call DataBind().

Upvotes: 0

Related Questions