2016-11-09 4 views
1

残高ラベルが最初に数値にバインドされた後、データソースを再度変更しても値は再び更新されません。DataBindingでラベルを更新する

データベースオブジェクトが変更された後、自動的にWindowsフォームラベルを更新したいのですが、constructorData.BankAccountに再プルします。でもConstructorDataクラスでINotifyPropertyChangedを実装し、

bankAccountBindingSource.DataSource = constructorData.BankAccount; 

あなたはBankAccountインスタンスに直接結合する:(フォームのロード内)ここにいるので

// 
// 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"; 
+1

わかりません。 – LarsTech

+0

@LarsTechビジュアルスタジオのデータソースツリーからラベルをドラッグして、自動的にバインディングソースを作成し、ラベルをバインドしました。ラベルのどのコードが役に立ちますか? – Ben

+0

[クリック](http://stackoverflow.com/q/1315621/1997232)。 – Sinatr

答えて

1

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); 
} 

自動生成コード(コメントに示唆されているように)助けにならないでしょう。そのデザインで

、あなたは(示されたコードのように)ConstructorData.BankAccountプロパティに新しいBankAccountインスタンスを割り当てて、いつでも、あなたが使用BindingSourceDataSourceとしてそれを設定することも必要があります。

constructorData.BankAccount = db.BankAccounts.Where(x => x.BankAccountId == constructorData.BankAccount.BankAccountId).SingleOrDefault(); 
// What do I do here 
bankAccountBindingSource.DataSource = constructorData.BankAccount; 
1

INotifyPropertyChangedを実装せずイワンの答えはまさにあなたが必要とするものです。

バインディングソースのDataSourceにこの方法でオブジェクトを配置するのは、BindingSource.DataSource = constructorData.BankAccountなので、BankAccountプロパティにあるオブジェクトをデータソースとして使用するためです。 constructorData.BankAccountの値を変更すると、データソースはBindingSourceに変更されず、以前のオブジェクトが含まれます。たとえば、次のコードを見てください。

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

bには現在何が含まれていますか? bにMyClass("1")が含まれていますか?確かにいいえ。詳細については

は、この記事を見てみましょう:

を私は問題を解決するためにINotifyPropertyChangedのを使用することはできますか?

[はい、この方法をConstructorDataINotifyPropertyChangedを実装し、バインディングを変更する場合は、次のラベルがあなたのコードであるが、あなたのConstructorDataクラスがINotifyDataChangingインターフェイスを実装すべき場所

bankAccountBindingSource.DataSource = constructorData; 
//... 
this.lblAccountBalance.DataBindings.Add(new System.Windows.Forms.Binding("Text", 
    this.bankAccountBindingSource, "BankAccount.Balance", true)); 
関連する問題