简体   繁体   English

如何在C#中刷新bindingnavigator绑定源?

[英]How to refresh the bindingnavigator binding source in c#?

I have a problem refreshing my bindingsource after I change the data on the database from an other form. 从其他形式更改数据库上的数据后,刷新bindingsource出现问题。 now when I first run my program all the data is shown in textboxes and the bindingnavigator has same records as the database. 现在,当我第一次运行程序时,所有数据都显示在textboxes ,并且bindingnavigator具有与数据库相同的记录。 With that being said, I'm trying to add or delete data from database in a form different from the the one which contain the bindingnavigator . 话虽这么说,我试图以一种不同于包含bindingnavigator的形式添加或删除数据库中的数据。 when I close the other forms and go back to the bindingnavigator form, the dataset is not updated, it only shows the data from the previous run of the application... 当我关闭其他表单并返回到bindingnavigator表单时, dataset未更新,它仅显示来自应用程序上次运行的数据...

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

The Fill() method of the TableAdapter only works when I run the program, I tried to implement it in other methods but it does not refresh my dataset . TableAdapterFill()方法仅在运行程序时起作用,我尝试以其他方法实现它,但不会刷新我的dataset Even if I close the form and reopen it, knowing that dataset loads on Form_Load() method. 即使我关闭窗体并重新打开它,也知道dataset加载到Form_Load()方法中。

I tried to make a reload method on a button somehow it sets the bindingnavigator binding source to null but no data are shown !!! 我试图在按钮上制作一个重载方法,以某种方式将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);

        }

Hints: 提示:

the Copy to output Directory property of the database is set to Copy Always database的“ Copy to output Directory属性设置为Copy Always

the Copy to output Directory property of the dataset is set to Do Not Copy dataset的“ Copy to output Directory属性设置为Do Not Copy

I'm using SqlServer 2008 for the database and visual studio 2010 for the project. 我将SqlServer 2008用于数据库,将visual studio 2010用于项目。 the database is a service-based database and the model used for the database is Entity Model 该数据库是基于service-based数据库,并且用于该数据库的模型是Entity Model

Well, since nobody can find a solution for this question I decided to do it myself... 好吧,由于没有人能找到这个问题的解决方案,所以我决定自己解决...

First off I had to delete the data bindings from all the controls from the window properties so I can create them programatically. 首先,我必须从window properties删除所有controlsdata bindings ,以便可以以编程方式创建它们。 then I had to implement a method that clears all data bindings from my textboxes and finally do the implementation of the UpdateBindingNavigator() method... 然后我必须实现一个方法,该方法清除我的textboxes所有data bindings ,最后执行UpdateBindingNavigator()方法的实现...

before you begin just define these two variables in the namespace; 在开始之前,只需在名称空间中定义这两个变量;

 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 you can call the method UpdateBindingNavigator() from anywhere you want and it will refresh your data with the new ones !!! 最后,您可以在任何需要的地方调用方法UpdateBindingNavigator() ,它将用新数据刷新您的数据!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM