我应该为...编写什么代码...在gridview中动态添加的按钮

问题描述:



我在gridview中添加了两个动态按钮.....

1:-批准
2:-拒绝

现在...我想要的是...当我单击拒绝"按钮时.该垂直行应从gridview中删除,即应从屏幕上消失.

我正在为您提供代码.请提供适用的代码或逻辑帮助我.

谢谢.......



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................

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"

    }
}

亲爱的朋友,

我已经给出了将新行添加到gridview中的解决方案,该概念与您可以应用逆向过程的概念相同,即在您的情况下删除行:-

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

我希望这能解决您的目的.如果您有帮助,请不要忘记将其标记为答案.

谢谢
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