2013-05-05 59 views
0

這裏我有一個窗體,其中包含一個datagrid視圖和三個按鈕,即編輯,刪除和返回。 Internally當我加載窗體它顯示DataGrid中正確的數據,但假設如果我編輯或刪除記錄通過單擊編輯或刪除按鈕,當這種形式來到執行這些操作後,前景DataGrid視圖isn沒有顯示更新的數據。 任何幫助將受到歡迎..... 注 - 此表單不會關閉,當我點擊編輯或刪除按鈕,它只是進入後臺。 我的代碼如下: -如何更新從後臺到前臺的表單時的DataGrid視圖?

namespace RDASMS 
{ 
    public partial class LoginDb : Form 
    { 
     DataTable dt = new DataTable(); 
     public LoginDb() 
     { 
      InitializeComponent(); 
     } 

     //OleDbConnection vcon = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\workspace\RDASMS\RDASMS\rdadb.mdb"); 

     public Form ReferToAdmin 
     { 
      get; 
      set; 
     } 

     private void LoginDb_Load(object sender, EventArgs e) 
     { 
      // TODO: This line of code loads data into the 'rdadbDataSet1.login' table. You can move, or remove it, as needed. 
      // this.loginTableAdapter.Fill(this.rdadbDataSet1.login); 
      // TODO: This line of code loads data into the 'rdadbDataSet.login' table. You can move, or remove it, as needed. 
      //this.loginTableAdapter.Fill(this.rdadbDataSet.login); 

      MyOleDbConnection.Open(); 
      DataSet ds = new DataSet(); 
      //DataTable dt = new DataTable(); 
      ds.Tables.Add(dt); 
      OleDbDataAdapter da = new OleDbDataAdapter(); 
      da = new OleDbDataAdapter("select * from login", MyOleDbConnection.vcon); 
      /*da.Fill(dt); 
      logindb_dataGridView.DataSource = dt.DefaultView;*/ 
      da.Fill(dt); 
      logindb_dataGridView.DataSource = dt; 
      logindb_dataGridView.AutoResizeColumns(); 
      MyOleDbConnection.Close(); 
     } 

     private void logindb_panel_Paint(object sender, PaintEventArgs e) 
     { 

     } 

     private void logindbback_Click(object sender, EventArgs e) 
     { 
      this.Close(); 
      this.ReferToAdmin.Show(); 
     } 

     private void logindbdelete_Click(object sender, EventArgs e) 
     { 
      DeleteRecord dr = new DeleteRecord(); 
      dr.ReferToLogindb = this; 
      dr.Show(); 
      this.Hide(); 
     } 

     private void logindbedit_Click(object sender, EventArgs e) 
     { 
      EditRecord er = new EditRecord(); 
      er.ReferToLogindb = this; 
      er.Show(); 
      this.Hide(); 
     } 
    } 
} 

回答

1

您可以使用窗體的「Activated」事件。這樣一來,每次的形式被激活的時間(「說到前景」,在你的情況下)的數據將被更新:

public LoginDb() 
    { 
     InitializeComponent(); 
     this.Activated += new EventHandler(LoginDb_Activated); 
    } 

    void LoginDb_Activated(object sender, EventArgs e) 
    { 
     this.BindData(); 
    } 

    private void BindData() 
    { 
     MyOleDbConnection.Open(); 
     DataSet ds = new DataSet(); 
     //DataTable dt = new DataTable(); 
     ds.Tables.Add(dt); 
     OleDbDataAdapter da = new OleDbDataAdapter(); 
     da = new OleDbDataAdapter("select * from login", MyOleDbConnection.vcon); 
     /*da.Fill(dt); 
     logindb_dataGridView.DataSource = dt.DefaultView;*/ 
     da.Fill(dt); 
     logindb_dataGridView.DataSource = dt; 
     logindb_dataGridView.AutoResizeColumns(); 
     MyOleDbConnection.Close(); 
    } 

請注意,這可能會導致數據庫,那麼你已經計劃更多的電話。你可以通過檢查是否有一個「編輯按鈕」被點擊,或者甚至更好,通過檢查數據是否被操縱(使用操作類的屬性)來避免這種情況: