Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

i have added two dynamic buttons with the gridview.....

1:-Approve
2:-Reject

now... what i want is ... when i click on "Reject" button. that perticular row should be deleted from the gridview i.e. it should disapper from the screen.

i m provideing you the code. please help me with appropriate code or logic i can apply.

thanks................

C#
protected void GVDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{

    //Adding the buttons
    Button apr = new Button();
    Button rej = new Button();
    apr.Text = "Approved";
    rej.Text = "Reject";

    //assignnig command name
    apr.CommandName = "Approved";
    rej.CommandName = "Reject";

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Cells[10].Controls.Add(apr);
        e.Row.Cells[11].Controls.Add(rej);
    }
}






protected void GVDetails_RowCommand(object sender, GridViewCommandEventArgs e)
{


    if (e.CommandName == "Approved")
    {

        GridViewRow row = (GridViewRow)((Control)e.CommandSource).NamingContainer;
         Id = Convert.ToInt32(GVDetails.DataKeys[row.RowIndex].Value.ToString()); // for this we have to set the "DataKeyNames="doc_id" in HTML file
        Response.Write("the doc_id is = " + Id);

        cn.Open();

        SqlCommand cmd = new SqlCommand("update tdocument set STAT=@STAT where doc_id=" + Id + "", cn);
        cmd.Parameters.Add("@STAT", SqlDbType.VarChar).Value = "APPROVED";
        cmd.ExecuteNonQuery();
       
        cn.Close();

        SendApprovedMails();  // calling the method to send mails to everyone
  
        Response.Redirect(Request.RawUrl);    // this line is to refresh the page without using "response.redirect"
    }
    else if (e.CommandName == "Reject") //HERE PLEASE TELL ME HOW SHOULD I CODE FOR DELETE            {
        Response.Write("Reject");
        GridViewRow row = (GridViewRow)((Control)e.CommandSource).NamingContainer;
         Id = Convert.ToInt32(GVDetails.DataKeys[row.RowIndex].Value.ToString()); // for this we have to set the "DataKeyNames="doc_id" in HTML file
        Response.Write("the doc_id is = " + Id);

        cn.Open();
        SqlCommand cmd = new SqlCommand("update tdocument set STAT=@STAT where doc_id=" + Id + "", cn);
        cmd.Parameters.Add("@STAT", SqlDbType.VarChar).Value = "Pending";
        cmd.ExecuteNonQuery();
        cn.Close();

        row.Visible = false;
        
        // code for deleteing the row from GridView
       
        
        sendRejectMails();

        Response.Redirect(Request.RawUrl);    // this line is to refresh the page without using "response.redirect"

    }
}
Posted
Updated 4-Mar-12 22:27pm
v2

1 solution

Dear Friend,

I have given the solution for adding new row into the gridview in this solution the same concept you can apply for the reverse process i.e., Delete of row in your case:-

http://www.codeproject.com/Answers/334007/Addign-new-row-to-a-gridview#answer1

I hope this will solve your purpose. Please don't forget to mark this as your answer if it helps you out.

Thanks
 
Share this answer
 
Comments
Tushar1999 5-Mar-12 5:35am    
thank you varun.....
Varun Sareen 5-Mar-12 6:10am    
your welcome tushar

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900