1

After my balance label is initially bound to a number, changing the datasource again doesn't update the value again.

I want to update the a Windows Form Label automatically after the database object is changed and I re-pull it into the constructorData.BankAccount.

public class ConstructorData
{
    public Client Client { get; set; }
    public BankAccount BankAccount { get; set; }
}

private void frmTransaction_Load(object sender, EventArgs e)
{
    // Pretend we populated constructor data already

    // This line of code is working
    bankAccountBindingSource.DataSource = constructorData.BankAccount;
}

private void lnkProcess_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
    constructorData.BankAccount = db.BankAccounts.Where(x => x.BankAccountId == constructorData.BankAccount.BankAccountId).SingleOrDefault();

    // What do I do here

    // Doesn't work
    bankAccountBindingSource.EndEdit();
    bankAccountBindingSource.ResetBindings(false);
}

Auto generated code:

// 
// lblAccountBalance
// 
this.lblAccountBalance.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.lblAccountBalance.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bankAccountBindingSource, "Balance", true));
this.lblAccountBalance.Location = new System.Drawing.Point(482, 71);
this.lblAccountBalance.Name = "lblAccountBalance";
this.lblAccountBalance.Size = new System.Drawing.Size(196, 23);
this.lblAccountBalance.TabIndex = 7;
this.lblAccountBalance.Text = "label1";
Ben
  • 2,122
  • 2
  • 28
  • 48
  • 1
    Not sure where the Label is in your code, but your ConstructorData class should be implementing an INotifyDataChanging interface. – LarsTech Nov 09 '16 at 16:22
  • @LarsTech I dragged the labels from the data source tree in visual studio so it automatically created a binding source and bound the label to it. Which code from the label would help? – Ben Nov 09 '16 at 16:25
  • [Click](http://stackoverflow.com/q/1315621/1997232). – Sinatr Nov 09 '16 at 16:33
  • Even if you implement `INotifyPropertyChanged` in `ConstructorData` using these settings you can not make it working. You should use the Ivan's answer or the change settings as I said in my answer. – Reza Aghaei Nov 09 '16 at 20:31

2 Answers2

2

Since here (inside the form load):

bankAccountBindingSource.DataSource = constructorData.BankAccount;

you bind directly to the BankAccount instance, even implementing INotifyPropertyChanged in ConstructorData class (as suggested in the comments) will not help.

With that design, anytime you assign a new BankAccount instance to the ConstructorData.BankAccount property (as in the shown code), you need also to set it as DataSource of the BindingSource used:

constructorData.BankAccount = db.BankAccounts.Where(x => x.BankAccountId == constructorData.BankAccount.BankAccountId).SingleOrDefault();
// What do I do here
bankAccountBindingSource.DataSource = constructorData.BankAccount;
Ivan Stoev
  • 195,425
  • 15
  • 312
  • 343
2

Without Implementing INotifyPropertyChanged Ivan's answer is exactly what you need.

The reason is because you put an object in DataSource of binding source this way: BindingSource.DataSource = constructorData.BankAccount, so it uses the object which is in BankAccount property as data source. If you change the value of constructorData.BankAccount, you disd't changed the data source of BindingSource and it will contain the previous object. For example take a look at this code:

var a = new MyClass("1");  // ← constructorData.BankAccount = something;
var b = a;                 // ← bindingSource.DataSource = constructorData.BankAccount.
a = new MyClass("2");      // ← constructorData.BankAccount = something else;

What should contain b now? Do you expect b contains MyClass("1")? Surely no.

For more information take a look at this post:

Can I use INotifyPropertyChanged to solve the problem?

If you implement INotifyPropertyChanged in ConstructorData and change bindings this way, yes:

bankAccountBindingSource.DataSource = constructorData;
//...
this.lblAccountBalance.DataBindings.Add(new System.Windows.Forms.Binding("Text",
    this.bankAccountBindingSource, "BankAccount.Balance", true));
Community
  • 1
  • 1
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398