將我的餘額標籤初始綁定到數字後,再次更改數據源不會再次更新該值。使用DataBinding更新標籤
我想在數據庫對象更改後自動更新Windows窗體標籤,並將其重新拉入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);
}
自動生成的代碼:
//
// 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";
不知道在哪裏的標籤是在你的代碼,但你ConstructorData類應實現的INotifyDataChanging接口。 – LarsTech
@LarsTech我從Visual Studio的數據源樹中拖動標籤,以便它自動創建一個綁定源並將標籤綁定到它。標籤中的哪些代碼可以提供幫助? – Ben
[點擊](http://stackoverflow.com/q/1315621/1997232)。 – Sinatr