2012-04-24 84 views
0

我只是在我的GridView中顯示我上傳的文件的詳細信息,所以在我的GridView中只有一行 - 不允許多個文件。Gridview顯示已刪除的數據

當我刪除單行並嘗試上載新文件時,它顯示2行(新文件​​和刪除的文件)。

我已經嘗試過使用GridView.DataSource = nullGridView.DataBind()

注意:我刪除後重新綁定了GridView,但仍顯示已刪除的文件。

protected void DeleteLinkButton_Click(object sender, EventArgs e) 
{ 
    if (Session["name"] != null) 
    { 
     string strPath = Session["filepath"].ToString(); 
     System.IO.File.Delete(strPath); 

     GridView2.Rows[0].Visible = false; 
     Label8.Text = ""; 
     Session["filename"] = null; 
     Button3.Enabled = true; 
    } 

    GridView2.DataBind(); 
} 
+0

您必須將其從網格的「DataSource」中移除。 – 2012-04-24 15:25:20

+0

我嘗試使用gridview.datasource = null和gridview.databind(),但沒有變化 – newuser1555 2012-04-24 15:31:40

+0

你在哪裏設置數據源到gridview?我只能看到** DataBind()** – 2012-04-24 15:38:59

回答

0

在某些情況下,我不得不做這樣的事情:

//page level variable 
bool refreshRequired = false; 

protected void DeleteLinkButton_Click(object sender, EventArgs e) 
{ 
    if (Session["name"] != null) 
    { 
     string strPath = Session["filepath"].ToString(); 
     System.IO.File.Delete(strPath); 

     GridView2.Rows[0].Visible = false; 
     Label8.Text = ""; 
     Session["filename"] = null; 
     Button3.Enabled = true; 

     refreshRequired = true; 
    } 

} 

protected void Page_PreRender(object sender, EventArgs e) 
{ 
    if(refreshRequired) 
    { 
     //whatever you to to set your grids dataset, do it here 
     //but be sure to get the NEW data 
    } 
} 

截至Delete的時候,你的網格綁定到舊數據。當您更改數據時,您必須將其重新綁定到新數據。