Why does a Bound Textbox not modify its associated property is the Text value is set programmatically?

c# data-binding entity-framework-6 textbox winforms

Question

A Windows Form I have has a lot of Textboxes on it. As the form loads, I attach them to a DataSource.

public partial class FormSettings : Form
{
    private readonly DbContext _context = new DbContext();

    public FormSettings()
    {
        InitializeComponent();
    }

    protected override void OnLoad(EventArgs e)
    {
        _context.OrderEntrySettings.Load();

        SettingsBindingSource.DataSource = _context.OrderEntrySettings.Local.ToBindingList();

        Binding saNbinding = new Binding("Text", SettingsBindingSource, "InvoiceNumber");

        InvoiceNumberTextBox.DataBindings.Add(saNbinding);

        Binding pthNbinding = new Binding("Text", SettingsBindingSource, "SalesOrderExportPath");

        PathTextBox.DataBindings.Add(pthNbinding);

    <snip>
    …   
    </snip>
}

An associated Textbox with a Directory Path (string) Property is present. The path saves completely well if I enter a new directory name in the Path Textbox and press the save button.

The Textbox Text displays the directory I selected if I alter the Text Property on the Textbox to the SelectedPath from a FolderBrowserDialog dialog, but the Entity is not changed, and nothing is saved back to the database.

I set the Path Textbox Text as well as the Property from the context to the SelectedPath as a workaround.

Why, if the Text value is entered programmatically, does a Bound Textbox not change the property that it is connected to?

1
1
6/29/2019 9:12:59 AM

Accepted Answer

ZZZ_tmp
6
10/29/2015 7:17:16 PM

Popular Answer

Using the shifts in control focus, data binding in WinForms (and possibly WebForms) can tell when the data in a field has changed.

In most cases, altering the control focus is not done when changing the values of the controls in the code-behind. Because of this, nothing alerts the data-binding engine that it needs to reassess the data, and nothing pokes it in the ribs.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow