1

bindingsource从其他表单更改数据库上的数据后,刷新我的问题。现在,当我第一次运行我的程序时,所有数据都显示在textboxes其中,并且bindingnavigator与数据库具有相同的记录。话虽如此,我正在尝试以不同于包含bindingnavigator. 当我关闭其他表单并返回bindingnavigator表单时,dataset它没有更新,它只显示应用程序上一次运行的数据......

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

唯一的Fill()方法TableAdapter在我运行程序时有效,我尝试在其他方法中实现它,但它不会刷新我的dataset. 即使我关闭表单并重新打开它,知道dataset加载Form_Load()方法。

我试图在按钮上创建一个重新加载方法,以某种方式将其设置 bindingnavigator binding sourcenull但没有显示数据!!!

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

        }

提示:

Copy to output Directory属性database设置为 Copy Always

Copy to output Directory属性dataset设置为Do Not Copy

我正在使用SqlServer 2008数据库和visual studio 2010项目。数据库是service-based数据库,用于数据库的模型是Entity Model

4

1 回答 1

3

好吧,既然没有人能找到这个问题的解决方案,我决定自己做......

data bindings首先,我必须从所有的中删除,controls以便window properties我可以以编程方式创建它们。然后我必须实现一个方法来清除data bindings我的所有内容textboxes,最后执行该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));

}

最后,您可以从任何您想要的地方调用该方法UpdateBindingNavigator(),它会用新的数据刷新您的数据!!!

于 2015-05-04T15:21:52.090 回答