2015-05-02 98 views
1

我在更改其他表單上的數據庫數據後,刷新我的bindingsource時出現問題。現在,當我第一次運行我的程序時,所有數據都顯示在textboxes中,而bindingnavigator與數據庫具有相同的記錄。這就是說,我試圖添加或刪除數據庫中的數據,其格式與包含bindingnavigator的格式不同。當我關閉其他形式和回到bindingnavigator形式,該dataset不更新,那隻能說明從以前的應用程序運行的數據...如何在c#中刷新bindingnavigator綁定源?

this.tblEmployeeTableAdapter.Fill(this.employeePayDatabaseDataSet.tblEmployee); 

TableAdapterFill()方法只有當我運行該程序時才起作用,我試圖用其他方法實現它,但它不刷新我的dataset。即使我關閉表單並重新打開它,知道dataset加載方法爲Form_Load()

我試圖讓一個按鈕重載方法在某種程度上它集 bindingnavigatorbinding sourcenull但沒有數據顯示!

private void bindingNavigatorReload_Click(object sender, EventArgs e) 
     { 
      EmployeePayDatabaseDataSetTableAdapters.tblEmployeeTableAdapter NewtblAdapter = new EmployeePayDatabaseDataSetTableAdapters.tblEmployeeTableAdapter(); 
      EmployeePayDatabaseDataSet NewDataSet = new EmployeePayDatabaseDataSet(); 
      NewtblAdapter.Fill(NewDataSet.tblEmployee); 

     } 

提示:

databaseCopy to output Directory屬性設置爲 Copy Always

datasetCopy to output Directory屬性設置爲Do Not Copy

我使用數據庫爲210,項目爲visual studio 2010。該數據庫是一個service-based數據庫,用於數據庫的模式Entity Model

回答

1

好,因爲沒有人可以找到這個問題,我決定自己做一個解決方案...

首先,我不得不從window properties中刪除所有controls中的data bindings,以便我可以通過編程方式創建它們。然後,我不得不實施掃清從我textboxes所有data bindings終於做UpdateBindingNavigator()方法的實現方法...

開始之前剛剛定義命名空間中的這兩個變量;

SqlDataAdapter datapter; 
DataSet dset 

string connection = "your_connection_string";

private void ClearBeforeFill() 
{ 
txtbox1.DataBindings.Clear(); 
txtbox2.DataBindings.Clear(); 
txtbox3.DataBindings.Clear(); 
txtbox4.DataBindings.Clear(); 
} 

private void UpdateBindingNavigator() 
{ 
ClearBeforeFill(); 
datapter = new SqlDataAdapter("SELECT * FROM tblEmployee", connection); 
dset = new DataSet(); 
datapter.Fill(dset); 

BindingSource1.DataSource = dset.Tables[0]; 

txtbox1.DataBindings.Add(new Binding("Text", BindingSource1, "Emp_ID", true)); 
txtbox2.DataBindings.Add(new Binding("Text", BindingSource1, "Emp_Name", true)); 
txtbox3.DataBindings.Add(new Binding("Text", BindingSource1, "Emp_Age", true)); 
txtbox4.DataBindings.Add(new Binding("Text", BindingSource1, "Emp_Salary", true)); 

} 

Finaly你可以從任何你想要 調用該方法UpdateBindingNavigator(),它會用新的更新您的數據!