【问题标题】:Change value of data inside databinding before displaying to textbox在显示到文本框之前更改数据绑定中数据的值
【发布时间】:2015-12-04 12:27:17
【问题描述】:

我的数据绑定设置与此相同 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...
}

My datagrid shows the Receipt model and when one is selected, my textboxes shows other details which are not displayed in the datagrid.

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

我首先使用实体​​框架代码。 请帮忙...

TIA。

【问题讨论】:

  • 您应该有某种方法可以使用所选收据中的 ProductId 获取产品实例。如果您使用的是 SQL,则应从中选择产品。
  • 你使用实体框架吗?
  • @RezaAghaei - 是的,我使用的是代码优先
  • 当你加载你的Receipt 时,也加载它的Product。然后您可以简单地使用它的Product.ToString() 或将TextBox 绑定到它的Product.ProductName
  • 如果您对答案有任何疑问,请告诉我:)

标签: c# winforms data-binding


【解决方案1】:

由于您使用的是实体框架并且您在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));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-26
    • 2010-09-09
    • 2014-03-13
    • 1970-01-01
    • 2012-10-18
    • 1970-01-01
    • 2011-09-13
    • 1970-01-01
    相关资源
    最近更新 更多