Search code examples
c#datasetbindingsourcetableadapterbindingnavigator

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. now when I first run my program all the data is shown in textboxes and the bindingnavigator has same records as the database. 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. 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...

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. Even if I close the form and reopen it, knowing that dataset loads on Form_Load() method.

I tried to make a reload method on a button somehow it sets the bindingnavigator binding source to null but no data are shown !!!

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

the Copy to output Directory property of the dataset is set to Do Not Copy

I'm using SqlServer 2008 for the database and visual studio 2010 for the project. the database is a service-based database and the model used for the database is Entity Model


Solution

  • 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. then I had to implement a method that clears all data bindings from my textboxes and finally do the implementation of the UpdateBindingNavigator() method...

    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 !!!