简体   繁体   English

将主/从绑定到主/从?

[英]Binding Master/Detail to Master/Detail?

My question is how does one tie two master/detail sources together.我的问题是如何将两个主/详细信息源联系在一起。 I am working in C# winforms and using VS 2017. My situation is I have 4 tables (LETTINGS, LINK_CONTRACT_LETTING, CONTRACTS, DESES) from an Access Database, that I am bringing into a dataset, which has four tables and three table relationships.我正在使用 C# winforms 并使用 VS 2017。我的情况是我有来自 Access 数据库的 4 个表(LETTINGS、LINK_CONTRACT_LETTING、CONTRACTS、DESES),我将它们带入一个具有四个表和三个表关系的数据集。 See Image.见图片。 Tables and Relationships表和关系

The CONTRACTS and DESES tables are bound to the textboxes on the form and these textboxes change to the next or previous record properly. CONTRACTS 和 DESES 表绑定到表单上的文本框,这些文本框正确地更改为下一条或上一条记录。 The binding source code for the CONTRACTS parent table is: CONTRACTS 父表的绑定源代码为:

//Contracts Binding
bsContracts = new BindingSource();
bsContracts.DataSource = dsPlanning;                    //DataSet
bsContracts.DataMember = contracts.TableName;       //DataTable

The binding source code for the Deses child table is: Deses子表的绑定源码为:

//Deses Binding
bsDeses = new BindingSource();              //Deses has a Details relationship to Contracts as in Master to Details. Therefore this binding is different.
bsDeses.DataSource = bsContracts;           //Master binding source bsContracts
bsDeses.DataMember = "ContToDes";       //"ContToDes" is the data relationship between the Contracts and Deses tables.

I also have done the same thing to the LETTINGS table and the LINK_CONTRACT_LETTING table master/detail like so:我也对 LETTINGS 表和 LINK_CONTRACT_LETTING 表 master/detail 做了同样的事情,如下所示:

//Lettings Binding
bsLettings = new BindingSource();
bsLettings.DataSource = dsPlanning;                     //DataSet
bsLettings.DataMember = lettings.TableName ;        //DataTable

//Links Binding
bsLinks = new BindingSource();
bsLinks.DataSource = bsLettings;        //Master binding source bsLettings
bsLinks.DataMember = "LetToLink";       //"ContToLink" is the data relationship between the Contracts and Link tables.

These additional 2 tables work together just like the first 2 tables.这些额外的 2 个表就像前 2 个表一样一起工作。

I am not sure how to link the LINK_CONTRACT_LETTING to the CONTRACTS table so that when a letting is selected only the contracts and deses associated with that letting are shown?我不确定如何将 LINK_CONTRACT_LETTING 链接到 CONTRACTS 表,以便在选择出租时仅显示与该出租相关的合同和设计? Is this possible or should I be doing something else?这是可能的还是我应该做其他事情? If I should be doing something else can you give me some direction?如果我应该做其他事情,你能给我一些指导吗?

I have tried changing the datasource and datamember for bsContracts and bsLinks, but so far nothing has worked.我尝试更改 bsContracts 和 bsLinks 的数据源和数据成员,但到目前为止没有任何效果。 Can I have a master/detail tied into another master/detail?我可以将一个主控/详细信息绑定到另一个主控/详细信息吗?

Thank you for your help.谢谢您的帮助。

Automatic filtering through binding only works from master to detail over a one-to-many relationship.通过绑定的自动过滤仅适用于一对多关系上的从主到详细信息。 When you have a third table linking two others in a many-to-many relationship, you have two masters and one detail.当您有第三张表以多对多关系链接另外两个表时,您有两个主表和一个明细表。 You cannot select a master record at one end and have that automatically filter the master records at the other end.您不能在一端选择主记录并让它自动过滤另一端的主记录。 You can automatically filter as far as the link table, but then you need to manually filter the other master.您可以自动过滤至链接表,但您需要手动过滤其他主控。 For instance, you can select a Letting record and have the BindingSource for the link table filter automatically.例如,您可以选择一条 Letting 记录并自动为链接表过滤器设置BindingSource You then need to gather the IDs from that BindingSource and use them to filter the BindingSource for the Contract table, eg然后,您需要从该BindingSource收集 ID 并使用它们来过滤 Contract 表的BindingSource ,例如

var contIds = bsLinks.Cast<DataRowView>().Select(drv => drv.Row.Field<int>("ContId"));

bsContracts.Filter = $"ContId IN ({string.Join(", ", contIds)})";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM