2012-02-22 134 views
2

我有一個dataGridView綁定到一個表。在與DataGridView相同的表單中,我有幾個文本框和一個按鈕,用於向數據庫添加新值並希望它們也顯示在gridView中。在數據庫中保存工作,只是新的值不顯示在gridView中。我曾嘗試以下:刷新DataGridView

this.Validate(); 
this.pBindingSource.EndEdit(); 
this.pTableAdapter.Update(this.DBDataSet); 
pDataGridView.Refresh(); 

這沒有奏效。從我的理解.Refresh方法只重繪GridView,所以它應該在更新tableAdapter後工作,所以我想。但事實並非如此。

然後我試着:

this.pDataGridView.DataSource = null; 
this.pDataGridView.DataSource = this.pBindingSource; 
pDataGridView.Refresh(); 

這也不起作用。然後嘗試將ResetBindings()作爲綁定源,但沒有奏效,然後我用完了想法。

如何刷新DataGridView?

+0

我認爲你需要使用:this.pBindingSource.DataSource – 2012-02-22 20:33:31

+0

它沒有工作。感謝您的輸入tho – 2012-02-22 20:46:05

+0

第一個問題:Silvelight還是WPF? 第二個:您用於數據收集的什麼類型 - pBindingSource? – Meonester 2012-02-22 20:58:17

回答

1

您可以修改像

this.pDataGridView.DataSource = null; 
this.pDataGridView.DataBind(); 
this.pDataGridView.DataSource = this.pBindingSource; 
pDataGridView.DataBind(); 
+0

pDataGridView沒有.DataBind()方法。我正在使用Visual Studio 2010 btw – 2012-02-23 09:05:47

+3

@orzacsergiu,只是你知道:VS的版本與c#的功能完全無關。我認爲你應該說明你的.Net框架的版本。 – gunr2171 2013-05-08 16:31:43

0

您也可以使用此:

this.[tablename]tableAdapter.Fill(this.[Database]DataSet.[tablename]); 
在每一個按鈕命令

,它至少爲我工作。

例如我的代碼看起來是這樣的:

this.studentinfoTableAdapter.Fill(this.studentDataSet.studentinfo);