【问题标题】:Remove GridView deleted row data from gridview directly直接从gridview中删除GridView删除的行数据
【发布时间】:2016-01-30 16:58:21
【问题描述】:

这是删除行的代码:

    else if (e.CommandName == "Deleterow")
                {
                    GridViewRow gr = (GridViewRow)
((Button)e.CommandSource).NamingContainer;      
                    SqlCommand com = new SqlCommand("StoredProcedure4", con);
                    com.CommandType = CommandType.StoredProcedure;
                    com.Parameters.AddWithValue("@ID", gr.Cells[0].Text);
                    com.ExecuteNonQuery();


                }

aspx:

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        SqlConnection con = Connection.DBconnection();
        if (e.CommandName == "EditRow")
        {
            GridViewRow gr = (GridViewRow)((Button)e.CommandSource).NamingContainer;               
            Textid.Text = gr.Cells[0].Text;
            Textusername.Text = gr.Cells[1].Text;
            Textclass.Text = gr.Cells[2].Text;
            Textsection.Text = gr.Cells[3].Text;
            Textaddress.Text = gr.Cells[4].Text;
        }
        else if (e.CommandName == "Deleterow")
        {
            GridViewRow gr = (GridViewRow)((Button)e.CommandSource).NamingContainer;      
            SqlCommand com = new SqlCommand("StoredProcedure4", con);
            com.CommandType = CommandType.StoredProcedure;
            com.Parameters.AddWithValue("@ID", gr.Cells[0].Text);
            com.ExecuteNonQuery();

        }
    }

当我删除按钮时,它在重新加载页面后删除了数据行。

所以我需要直接从 gridview 中删除该行。

为此该怎么办,有人可以帮忙吗?

谢谢,

【问题讨论】:

  • 无需重新加载页面,只需在 com.ExecuteNonQuery(); 之后重新绑定您的网格即可;
  • 不要使用Response.Redirect()重新绑定你的gridview
  • @Rani:你的 Page_Load 内容。这将更容易让我们为您提供适当的解决方案。
  • 好的,只需分享您填充网格视图的部分,例如GridView1.Datasource 正在被调用。
  • 可以GridView1.DeleteRow(gr.Cells[0].Text); 解决您的问题。stackoverflow.com/questions/11473226/…

标签: c# asp.net gridview


【解决方案1】:
            else if (e.CommandName == "Deleterow")
            {
                GridViewRow gr = (GridViewRow)
                ((Button)e.CommandSource).NamingContainer;      
                SqlCommand com = new SqlCommand("StoredProcedure4", con);
                com.CommandType = CommandType.StoredProcedure;
                com.Parameters.AddWithValue("@ID", gr.Cells[0].Text);
                com.ExecuteNonQuery();
                //Response.Redirect("studententry.aspx"); Remove this line
                //call method here you have used to populate the gridview at page load.  
            }                                

【讨论】:

  • 答案中的这一行,在此处重新绑定网格。?
  • 您在页面加载时用于填充 gridview 的方法调用该方法
  • @Rani,如何第一次绑定gridview?
  • 您在回答中说,此处的调用方法用于在页面加载时填充网格视图。 .但我在page_load中没有代码..
  • @Rani,好的。但是使用哪种方法来绑定gridview。同样的代码放在那里。
猜你喜欢
  • 2016-05-25
  • 1970-01-01
  • 2013-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多