DataGridView 中外键的组合框 [英] Combobox for Foreign Key in DataGridView

查看:32
本文介绍了DataGridView 中外键的组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两个表,产品和许可证的数据库.Licences.ProductID 具有对 Products.ProductID 的外键引用(即该产品的许可证).

I have a database containing two tables, Products and Licences. Licences.ProductID has a foreign key reference to Products.ProductID (i.e. licenses for that product).

如何在 WinForms DataGridView 中表示这种关系?

How do I represent that relationship in a WinForms DataGridView?

当提供 DataGridView(SQL Metal 和通过 LINQ to SQL)ProductLicences.ProductID 时,它会自动生成一个带有文本字段的列,需要一个产品"(我当然不能输入......).

When feeding the DataGridView (SQL Metal and through LINQ to SQL), the ProductLicences.ProductID, it automatically generates a column with a text field expecting a "Product" (which of course I can't enter...).

如何更改此列以包含列出可用产品的组合框?

How do I change this column to contain a combobox listing the available products?

我有一个连接(继承自Linq.DataContext),分配给DataGridView的数据源是一个Link.IQueryable,生成如下:

I have an connection (inherits from Linq.DataContext), the data source assigned to the DataGridView is a Link.IQueryable, generated as such:

var ds = from c in m_connection.Licences
    select c;

推荐答案

在这种情况下,您尝试模仿的行为是 Lookup Combo.您不想使用 License 类的 Product 字段,您实际上想使用 ProductID 字段.

In this case, the behaviour you are trying to imitate is a Lookup Combo. You don't want to use the Product field of the License class, you actually want to use the ProductID field.

这是一个快速的分步:

  1. 删除网格中的Product列;仅保留 ProductID.

ProductID 列的 ColumnType 属性更改为 DataGridViewComboBoxColumn.

Change the ProductID column's ColumnType property to DataGridViewComboBoxColumn.

将此列的 DataSource 更改为新的 BindingSource,并将 DataSource 设置为 MyProject.Product(您可以按照向导操作,可能与您为网格本身所做的相同,但使用 Product 而不是 License.

Change this column's DataSource to a new BindingSource with the DataSource set to MyProject.Product (you can just follow the wizard, probably the same way you did it for the grid itself, but using Product instead of License).

将此列的 DisplayMember 更改为要在组合框中显示的任何文本,例如 ProductName.

Change the DisplayMember of this column to whatever text you want to show in the combo box, for example, ProductName.

ValueMember 更改为实际的主键,例如 ProductID.

Change the ValueMember to the actual primary key, such as ProductID.

填充GridView本身之前,用产品数据初始化新的productsBindingSource,即用productBindingSource.DataSource =context.Products;.

Before populating the GridView itself, initialize the new productsBindingSource with product data, i.e. with productBindingSource.DataSource = context.Products;.

就是这样.现在,当您 SubmitChanges(假设您一直保持 DataContext 打开)时,它将使用正确的引用更新 ProductID.请注意,它可能不会更新它保存的类上的 Product 引用;如果出于任何原因需要使用实际实体引用,则可能需要先对其调用 DataContext.Refresh 方法.但是不用担心这个,除非你需要在网格之外使用实体类.

That's it. Now when you SubmitChanges (assuming you kept the DataContext open the whole time), it will update the ProductID with the correct reference. Note that it might not update the Product reference on the class that it saved; if you need to make use of the actual entity reference for any reason then you might need to call the DataContext.Refresh method on it first. But don't worry about this unless you need to use the entity class outside the grid.

这篇关于DataGridView 中外键的组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆