Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Data not updated unless controls loose focus first.
 

Data not updated unless controls loose focus first.

For Example:

private BindingList<Company> companies;
private System.Windows.Forms.BindingSource bindingSource;

        private void CompanyForm_Load(object sender, EventArgs e)
        {
            companies = new BindingList<Company>(PersistenceManager.Instance.RetrieveAll<Company>(SessionAction.BeginAndEnd));
            companies.AllowNew = true;
            companies.AllowEdit = true;

            bindingSource.DataSource = companies;

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

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

            Company company = bindingSource.Current as Company;

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




Unless I tab out of the textbox before I click the save button, mybindingSource.Current has not been updated with the text I entered into it.
What am I doing wrong here?
zenox  Saturday, September 12, 2009 6:03 PM
Try call bindingSource.EndEdit first before save.
John Chen -- See my team blog: http://blogs.msdn.com/vsdata. All my posts are provided "AS IS" with no warranties, and confer no rights.
John Chen MS  Saturday, September 12, 2009 8:40 PM
Another thing you might want tolook into setting (DataBindings)\(Advanced)\Data Source Update Mode to OnPropertyChanged in the properties for the binding.

By default, this is set to OnValidation which means the data sourceis updatedas part of the Windows Forms Control validation process when a control is exited (or when you call EndEdit to force this to happen earlier).

(For rows being newly added, you will still need an EndEdit at some point to actually get the DataRow into the DataTable. But at least the in-memory data for the DataRow will be updated earlier.)
BinaryCoder  Saturday, September 12, 2009 11:42 PM
Try call bindingSource.EndEdit first before save.
John Chen -- See my team blog: http://blogs.msdn.com/vsdata. All my posts are provided "AS IS" with no warranties, and confer no rights.
John Chen MS  Saturday, September 12, 2009 8:40 PM
Another thing you might want tolook into setting (DataBindings)\(Advanced)\Data Source Update Mode to OnPropertyChanged in the properties for the binding.

By default, this is set to OnValidation which means the data sourceis updatedas part of the Windows Forms Control validation process when a control is exited (or when you call EndEdit to force this to happen earlier).

(For rows being newly added, you will still need an EndEdit at some point to actually get the DataRow into the DataTable. But at least the in-memory data for the DataRow will be updated earlier.)
BinaryCoder  Saturday, September 12, 2009 11:42 PM

You can use google to search for other answers

Custom Search

More Threads

• Populating datagridview in C#
• DateTimePicker Problems
• Listbox Question
• Saving directly to access database from textboxes
• How to insert, update, delete using DataGrid ?
• Drop Down or Combo Box
• ASP.Net Related Questions
• Populating datagridview manually vs. binding
• A way to change the values of a collection of selected (i.e. highlighted) checkboxcells in a DataGridViewCheckBoxColumn?
• Combobox selected index in datagridview