在与多个父实体记录相关的DataGridView控件中显示子数据

时间:2013-07-22 01:32:22

标签: vb.net winforms entity-framework entity-framework-5

我有一个带有三个BindingSource控件的VB winforms表单,每个控件都显示来自三个创建了DB First的相关实体的数据(通过DataGridView s或绑定文本框)。

我需要datagridview显示&仅允许添加/编辑/删除与当前tbl_Distribution_DealerModel_OverAlts记录(加入字段tbl_PrimaryDealerGroup)和当前tbl_PrimaryDealerGroup.DealerGroupID = tbl_Distribution_DealerModels_OverAlts.FirstDealerGroupID记录相关的tbl_SeriesManufacturer条记录(加入两个常见记录)字段:aus_series_cdeManufacturerID)。 tbl_Distribution_DealerModel_OverAlts与其他两个表的关系在很多方面:

Entities

如何让子实体的datagridview仅显示与两个(或更多)父实体记录中的每一个相关的记录?我可能还需要在这个项目中将儿童实体与三个或更多的父母联系起来,因此一般适用的答案将不胜感激

1 个答案:

答案 0 :(得分:0)

我终于把它排除在外:

Private Sub bs_tblSM_tblPDG_CurrentChanged(sender As System.Object, e As System.EventArgs) Handles bs_tbl_Series_Manufacturer.CurrentChanged, bs_tbl_PrimaryDealerGroup.CurrentChanged
    If bs_tbl_Series_Manufacturer.Current IsNot Nothing And bs_tbl_PrimaryDealerGroup.Current IsNot Nothing Then
        Dim Series As String = bs_tbl_Series_Manufacturer.GetItemProperties(Nothing)("aus_series_cde").GetValue(bs_tbl_Series_Manufacturer.Current)
        Dim Manufacturer As Byte = bs_tbl_Series_Manufacturer.GetItemProperties(Nothing)("ManufacturerID").GetValue(bs_tbl_Series_Manufacturer.Current)
        Dim PDG As Integer = bs_tbl_PrimaryDealerGroup.GetItemProperties(Nothing)("DealerGroupID").GetValue(bs_tbl_PrimaryDealerGroup.Current)
        Dim ChangeFilter As Boolean = False
        If m_CurrentSeries <> Series Then
            m_CurrentSeries = Series
            ChangeFilter = True
        End If
        If m_CurrentManufacturer <> Manufacturer Then
            m_CurrentManufacturer = Manufacturer
            ChangeFilter = True
        End If
        If m_CurrentPDG <> PDG Then
            m_CurrentPDG = PDG
            ChangeFilter = True
        End If
        If ChangeFilter Then
            Dim DlrMdlOverAlts As IEnumerable(Of tbl_Distribution_DealerModel_OverAlts)
            DlrMdlOverAlts = From DDMOA As tbl_Distribution_DealerModel_OverAlts In m_Context.tbl_Distribution_DealerModel_OverAlts.Local.ToBindingList _
                             Where DDMOA.aus_series_cde = m_CurrentSeries And DDMOA.ManufacturerID = m_CurrentManufacturer And DDMOA.FirstDealerGroupID = m_CurrentPDG _
                             Select DDMOA
            bs_tbl_Distribution_DealerModel_OverAlts.DataSource = DlrMdlOverAlts.ToList
            Me.dgvtbcDealerModelOverAlts_aus_series_cde.DefaultCellStyle.NullValue = m_CurrentSeries.ToString
            Me.dgvtbcDealerModelOverAlts_ManufacturerID.DefaultCellStyle.NullValue = m_CurrentManufacturer.ToString
            Me.dgvtbcDealerModelOverAlts_FirstDealerGroupID.DefaultCellStyle.NullValue = m_CurrentPDG.ToString
            bs_tbl_Distribution_DealerModel_OverAlts.ResetBindings(False)
            ChangeFilter = False
        End If
    End If
End Sub

诀窍是不要尝试在子DataMember中使用BindingSource个父BindingSource个},而是将两个CurrentChanged中代码中的实体关联起来父BindingSource s。