我的問題是如何將兩個主/詳細資訊源聯系在一起。我正在使用 C# winforms 并使用 VS 2017。我的情況是我有來自 Access 資料庫的 4 個表(LETTINGS、LINK_CONTRACT_LETTING、CONTRACTS、DESES),我將它們帶入一個具有四個表和三個表關系的資料集。見圖片。表和關系
CONTRACTS 和 DESES 表系結到表單上的文本框,這些文本框正確地更改為下一條或上一條記錄。CONTRACTS 父表的系結源代碼為:
//Contracts Binding
bsContracts = new BindingSource();
bsContracts.DataSource = dsPlanning; //DataSet
bsContracts.DataMember = contracts.TableName; //DataTable
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.
我也對 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.
這些額外的 2 個表就像前 2 個表一樣一起作業。
我不確定如何將 LINK_CONTRACT_LETTING 鏈接到 CONTRACTS 表,以便在選擇租賃時僅顯示與該租賃相關的合同和設計?這是可能的還是我應該做其他事情?如果我應該做其他事情,你能給我一些指導嗎?
我嘗試更改 bsContracts 和 bsLinks 的資料源和資料成員,但到目前為止沒有任何效果。我可以將一個主控/詳細資訊系結到另一個主控/詳細資訊嗎?
謝謝您的幫助。
uj5u.com熱心網友回復:
通過系結的自動過濾僅適用于一對多關系上的從主到詳細資訊。當您有第三個表以多對多關系鏈接另外兩個表時,您有兩個主表和一個明細表。您不能在一端選擇主記錄并讓它自動過濾另一端的主記錄。您可以自動過濾至鏈接表,但您需要手動過濾其他主控。例如,您可以選擇一條 Letting 記錄并BindingSource自動過濾鏈接表。然后,您需要從中收集 IDBindingSource并使用它們來過濾BindingSourceContract 表,例如
var contIds = bsLinks.Cast<DataRowView>().Select(drv => drv.Row.Field<int>("ContId"));
bsContracts.Filter = $"ContId IN ({string.Join(", ", contIds)})";
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/489373.html
上一篇:單擊滑鼠后使文本框文本消失
下一篇:C#SQL從資料庫中獲取圖片
