【问题标题】:Entity Framework How to bind textbox to model in Windows Forms实体框架如何将文本框绑定到 Windows 窗体中的模型
【发布时间】:2012-04-27 09:16:14
【问题描述】:

在 Windows 窗体项目中使用实体数据模型,我想同时将 Orders 实体绑定到 datagridview 和 textBox,并且 textBox 必须根据数据网格中的当前行显示 OrderID 值。

我在表单加载事件中使用的代码如下:

    using(NorthwindEntities context = new NorthwindEntities())
    {
    ordersDataGridView.DataSource = context.Orders;
    OrderNumberTextBox.   ...
    }

对于这种情况,绑定 Textbox 的正确语法是什么?谢谢。

【问题讨论】:

  • textBox 没有数据源,需要手动绑定
  • 我没有说textBox有datasource属性,我的问题是如何让它显示字段值?此文本框已从数据源窗口拖放到表单中。

标签: c# winforms entity-framework linq-to-entities linq-to-objects


【解决方案1】:

BindingSource 对象绑定到context.Orders,将DataGridView 绑定到BindingSource,然后通过TextBox.DataBindings 属性,将TextBox 的相应属性绑定到BindingSourceBindingSource 对象将管理货币状态,因此当您在 DataGridView 中选择不同的项目时,TextBox 会发生变化。

绑定看起来类似于以下内容:

OrderNumberTextBox.DataBindings("Text", bindingSource, "OrderID");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-07
    • 2015-06-21
    • 2016-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多