在显示到文本框之前更改数据绑定内的数据值
本文关键字:数据绑定 数据 显示 文本 | 更新日期: 2023-09-27 18:25:32
我有与此相同的数据绑定设置Visual Studio Winform设计器:在当前控件BindingSource 上设置DataBindings
但我不知道如何改变的价值,比方说我有两个模型:
class Receipt {
public int ProductId { get; set; }
public double Price { get; set; }
//etc...
}
class Product {
public int ProductId { get; set; }
public string ProductName { get; set; }
//etc...
}
我的数据网格显示Receipt模型,当选择一个时,我的文本框显示数据网格中未显示的其他详细信息。
现在我的问题是,我需要在文本框中显示ProductName,而不是ProductId。
我首先使用实体框架代码。请帮忙。。。
TIA。
由于您使用的是实体框架,并且Receipt
类中有Product
属性,因此可以用Receipt
加载Product
,例如:
this.receiptBindingSource.DataSource = db.Receipt.Include("Product").ToList();
您可以使用设计器或代码将TextBox
的数据绑定设置为绑定到Product.ProductName
属性:
this.textBox1.DataBindings.Add(new System.Windows.Forms.Binding("Text",
this.receiptBindingSource, "Product.ProductName", true));