2012-06-04 106 views
4

如何在關閉子窗體後對數據庫進行更改後刷新datagridview我嘗試使用click事件刷新datagridview,但它不工作,請問我必須使用數據集?從子窗體更新數據庫後刷新datagridview win表格

  //create an OleDbDataAdapter to execute the query 
      dAdapter = new OleDbDataAdapter(gQuery, connString); 

      //create a command builder 
      cBuilder = new OleDbCommandBuilder(dAdapter); 

      //create a DataTable to hold the query results 
      dTable = new DataTable(); 

      //fill the DataTable 
      dAdapter.Fill(dTable); 


      //BindingSource to sync DataTable and DataGridView 
      bSource = new BindingSource(); 

      //set the BindingSource DataSource 
      bSource.DataSource = dTable; 


      //set the DataGridView DataSource 
      dataGridView1.DataSource = bSource; 

    private void button_Refresh_Click(object sender, EventArgs e) 
    { 

     dataGridView1.DataSource = bSource; 
     dataGridView1.Refresh(); 


    } 

幫助我,請提前

+0

的可能重複的[簡單的DataGridView刷新問題](http://stackoverflow.com/questions/253843/simple-datagridview-refresh-question) –

+0

http://stackoverflow.com/questions/9790676/why -does-my-datagridview -refuse-to-refresh/9791331#9791331 –

回答

0

感謝你試過

dataGridView1.DataSource = dTable; 
+0

是累了,它不刷新DataGridview :-( – sevoug

8

添加

dataGridView1.Update(); 

這將解決您的問題。

+0

更新方法的竅門...謝謝尼廷。:) – Bravo

0
bSource.DataSource = dTable; 
dataGridView1.DataSource = bSource; 

它會更好,如果你還記得你的表

3

當你的DataGridView的屬性與「數據源」鏈接數據庫,IDE會自動將的BindingSource和TableAdapter的到表單中。

如果更新數據庫,並要刷新的DataGridView,稱之爲:

this.<table name>TableAdapter.Fill(this.<DB name>DataSet.<table name>); 

哪裏<table name>是你的表的名稱(例如用戶)和<DB name>是你的數據庫(例如MYDB)的名稱。

this.UsersTableAdapter.Fill(this.MyDBDataSet.Users); 
+0

很好的答案。我一直在尋找這個。謝謝! – usefulBee