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

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

问题描述

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



我如何代表一个WinForms DataGridView的这种关系?



在喂食的DataGridView(SQL金属并通过LINQ到SQL)的ProductLicences.ProductID,它会自动与文本字段期待一个产品(当然我不能进入...)生成一个列。



如何我更改此列包含一个组合框列出了可用的产品呢?






我有一个连接(继承自 Linq.DataContext ) ,被分配到DataGridView的数据源是一个 Link.IQueryable ,因为这样产生的:

  VAR DS =从C在m_connection.Licences 
选择C;


解决方案

在这种情况下,行为,你正在试图模仿是查找组合。你不想使用产品许可类,你真的想使用<$ C场。$ C>的ProductID 字段



下面是一个简单的一步一步的:




  1. 删除产品在网格列;只保留的ProductID


  2. 更改的ProductID 列的 ColumnType 属性为 DataGridViewComboBoxColumn


  3. 更改此列的数据源来一个新的的BindingSource 通过设置为<数据源 code> MyProject.Product (你可以按照向导,很可能你做到了网格本身同样的方式,但使用产品而不是许可)。


  4. 更改的DisplayMember 此列您要在组合框中显示,无论文字,例如产品名称


  5. 更改 ValueMember 来实际的主键,如的ProductID


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




这就是它。现在,当你的SubmitChanges (假设你不停的的DataContext 开全时),它将更新的ProductID 用正确的参考。请注意,它可能无法更新产品上,它保存了类参考;如果你需要使用以任何理由实际的实体引用,那么你可能需要先调用 DataContext.Refresh 方法就可以了。但是,除非你需要使用实体类的网格外不担心这一点。


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

How do I represent that relationship in a WinForms DataGridView?

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?


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;

解决方案

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.

Here's a quick step-by-step:

  1. Remove the Product column in the grid; keep only the ProductID.

  2. Change the ProductID column's ColumnType property to DataGridViewComboBoxColumn.

  3. 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).

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

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

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

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天全站免登陆