tags:

views:

166

answers:

2

I have a binding navigator which I am using to add/edit items. The problem I'm having is when I click the save button (button I added to the toolbar) my bindings dont seem to have taken effect. This code loads and binds my data.

        private void CompanyForm_Load(object sender, EventArgs e)
        {
            // Loads all of the companies
            companies = new BindingList<Company>(PersistenceManager.Instance.RetrieveAll<Company>()));
            companies.AllowNew = true;
            companies.AllowEdit = true;

            bindingSource.DataSource = companies;

            // Add our bindings
            companyIdTextBox.DataBindings.Add("Text", companies, "Id");
            companyNameTextBox.DataBindings.Add("Text", companies, "Name");
        }

And this code is run when the save button is clicked:

        private void toolStripSaveButton_Click(object sender, EventArgs e)
        {
            // If we have no binding source, then we cannot save
            if (null == bindingSource.Current)
            {
                return;
            }

            Company company = bindingSource.Current as Company;

            // Save our company
            PersistenceManager.Instance.Save<Company>(company);
        }

The problem is when I click the save button, the current item is never updated with the data from the inputs.

Am I just missing something stupid?

+2  A: 

When adding your bindings, try doing it like this ...

companyIdTextBox.DataBindings.Add("Text", companies, "Id", true, DataSourceUpdateMode.OnPropertyChanged);
companyNameTextBox.DataBindings.Add("Text", companies, "Name", true, DataSourceUpdateMode.OnPropertyChanged);

By default, the DataSourceUpdateMode is set to OnValidation; perhaps the OnValidation event is not firing in your case. If that's the problem, setting the DataSourceUpdateMode to OnPropertyChanged should do the trick.

Steve Dignan
A: 

I assume your persistence store is a database ? In VS solution explorer, look at the properties of your database file, and check that "Copy to output directory" is not set to "Copy always". This would overwrite your database in the working directory every time you build the project...

Thomas Levesque