在显示到文本框之前更改数据绑定内的数据值

时间:2015-12-04 03:50:10

标签: c# winforms data-binding

我有与此相同的数据绑定设置 Visual Studio Winform designer: Set DataBindings on current control BindingSource

但我不知道如何改变让我们说有2个模特的价值:

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...
}

我的数据网格显示了收据模型,当选择一个时,我的文本框会显示未在数据网格中显示的其他详细信息。

现在我的问题是我需要在文本框中显示ProductName而不是ProductId。

我正在使用Entity Framework Code First。 请帮忙......

TIA。

1 个答案:

答案 0 :(得分:1)

由于您使用的是Entity Framework且Product类中有Receipt属性,因此可以Product加载Receipt,例如:

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