简体   繁体   English

使用C#从DetailView删除数据后刷新GridView

[英]Refresh GridView After Data is Deleted From DetailView Using C#

When user select any record from the GridView then my DetailView is updated based on the selection of the GridView. 当用户从GridView中选择任何记录时,我的DetailView将根据GridView的选择进行更新。 So what I am trying to do is that when I delete anything from the DetailView then I want to refresh the GridView so basically I don't want to show still the deleted record in the GridView. 所以我想要做的是,当我从DetailView中删除任何东西然后我想刷新GridView所以基本上我不想在GridView中显示已删除的记录。 I have tried to resolve this issue by doing the data bind after my connection and SQL statement but it does not refresh it. 我试图通过在连接和SQL语句之后执行数据绑定来解决此问题,但它不会刷新它。 One thing to note is that I am using a Accordion pane but both my gridview and the detailview are on the same pane. 需要注意的一点是我使用的是Accordion窗格,但我的gridview和detailview都在同一个窗格中。 I am not sure if this is breaking anything. 我不确定这是否会破坏任何东西。 Here is my code: 这是我的代码:

protected void Refresh_ItemCommand(object sender, DetailsViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("Delete", StringComparison.CurrentCultureIgnoreCase))
        {

            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
            SqlDataAdapter da = new SqlDataAdapter("select ID, Name, Address from dbo.MyTable", con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            Gridview1.DataSource = dt;
            Gridview1.DataBind();
        }

    }

You may use the event of the Data view called "ItemDeleted" as follows: 您可以使用名为“ItemDeleted”的数据视图的事件,如下所示:

  DetailViewName_ItemDeleted(object sender, 
    DetailsViewDeletedEventArgs e)
  {
    // Refresh the GridView control after a new record is updated 
    // in the DetailsView control.
    GridViewName.DataBind();
  }

The above code is from the official MSDN site for detail view control. 以上代码来自官方MSDN网站,用于详细视图控制。

The other way (which I prefer) is to handle the data grid during the Page_load procedure so when you press your delete button in the detail view, the page will perform a postback. 另一种方式(我更喜欢)是在Page_load过程中处理数据网格,因此当您在详细视图中按下删除按钮时,页面将执行回发。

So in the procedure Page_load you can call another procedure which fills the data grid. 因此,在过程Page_load您可以调用另一个填充数据网格的过程。 The code might be like this: 代码可能是这样的:

if (isPostback)
{
  FillGrid();    
} 

private void FillGrid()
{

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
            SqlDataAdapter da = new SqlDataAdapter("select ID, Name, Address from dbo.MyTable", con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            Gridview1.DataSource = dt;
            Gridview1.DataBind();
}

Your code doesn't show anything where the record is actually deleted so it's understandable that when you re-fetch the data still contains everything. 您的代码没有显示实际删除记录的任何内容,因此当您重新获取数据时仍然包含所有内容是可以理解的。

What you need to do is: 你需要做的是:

  1. Execute a sql statement to delete the record 执行sql语句删除记录
  2. Re-fetch the data 重新获取数据
  3. Rebind the data. 重新绑定数据。

If you follow those 3 steps it will work. 如果您按照这3个步骤进行操作。

try to use the event that is called in case of pressing the delete key from the DGV 尝试使用从DGV按下删除键时调用的事件

private void DGV_DeleteKeyPressed(object sender, KeyEventArgs e)
{
    //enter code here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM