0

一点背景知识,我是一名 Web 开发人员,正在开发他的第一个Winform.

我正在使用EF5. 这些表是关系的,但是这些表只有一个主键。我有Winform一个GridView附在它上面的。这GridView是由一个BindingSource. 的DatasourceforBindingSource正在由 Linq 查询填充。


Public Class Form1
    Private batchEnt As BatchMananger.PrintManagerEntities
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        Try

            batchEnt = New BatchMananger.PrintManagerEntities

            batchBindingSource.DataSource = (From b In batchEnt.AutomatedBatches Join bd In batchEnt.AutomatedBatchResults On b.ID Equals bd.AutomatedBatchID Where b.BatchName <> "" Select b.ID, b.BatchName, b.Description, b.ScheduleDesc, b.BatchResultEmail, b.BatchSourceEmail, bd.ExecutionDateTime, bd.TotalSuccesful, bd.TotalItems, bd.TotalFail).ToList
        Catch ex As Exception
            Throw
        End Try
    End Sub
End Class

我的问题是Gridview只填充了AutomatedBatches表中的数据,而不是连接表中的数据AutomatedBatchResults。但是,在检查Datasource元素时,我看到连接的结果又回来了。我将如何绑定到 ,BIndingsource以便Linq查询的所有结果都填充到我的Gridview.

如果您需要更多信息,请告诉我。

更新

发现出了什么问题。我没有在 GridView 的属性上设置 DataPropertyName。将 DataPropertyName 设置为它工作的数据库字段的相同名称后。我还在 EF 数据模型中创建了 AutomatedBatch (1) 与 AutomatedBatchResult (many) 之间的关联。

4

2 回答 2

0

在没有看到您的数据的情况下,猜测是您的联接请求导致联接表中的某些记录不显示。记住linq中的join关键字是inner join,所以任何不满足join条件的记录都不会显示。要显示表中的所有记录,我会执行以下操作:

快速注释——请在所有对象上添加类型。

Private batchEnt As BatchMananger.PrintManagerEntities
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load

        Try
            Dim batchEnt As PrintManagerEntities = New PrintManagerEntities
            Dim batchBindingSource

            Dim batcheswithResults = (From b In batchEnt.AutomatedBatches
                                             Join bd In batchEnt.AutomatedBatchResults  On b.ID Equals bd.AutomatedBatchID
                                             Where b.BatchName <> ""
                                             Select New Results(b, bd)).ToList

            Dim batchesWithoutResults = (From b In batchEnt.AutomatedBatches
                                         Where Not batchEnt.AutomatedBatchResults.Any(Function(o) o.Id = b.Id)
                                         Select New Results(b, Nothing)).ToList

            Dim itemsSource As New List(Of Object)
            itemsSource.AddRange(batcheswithResults)
            itemsSource.AddRange(batchesWithoutResults)

            batchBindingSource.DataSource = itemsSource

        Catch ex As Exception
            Throw
        End Try
    End Sub

End Module

Public Class Results
    'TODO: Add types to params and all properties
    Public Sub New(AutomaticBatches, AutomatedBatchResults)
        _automaticBatches = AutomaticBatches
        _automatedBatchResults = AutomatedBatchResults
    End Sub

    Private _automaticBatches
    Private _automatedBatchResults

    Public Property ID
        Get
            Return _automaticBatches.ID
        End Get
        Set(value)
            _automaticBatches.ID = value
        End Set
    End Property
    Public Property BatchName
        Get
            Return _automaticBatches.BatchName
        End Get
        Set(value)
            _automaticBatches.BatchName = value
        End Set
    End Property
    Public Property Description
        Get
            Return _automaticBatches.Description
        End Get
        Set(value)
            _automaticBatches.Description = value
        End Set
    End Property
    Public Property ScheduleDesc
        Get
            Return _automaticBatches.ScheduleDesc
        End Get
        Set(value)
            _automaticBatches.ScheduleDesc = value
        End Set
    End Property
    Public Property BatchResultEmail
        Get
            Return _automaticBatches.BatchResultEmail
        End Get
        Set(value)
            _automaticBatches.BatchResultEmail = value
        End Set
    End Property
    Public Property BatchSourceEmail
        Get
            Return _automaticBatches.BatchSourceEmail
        End Get
        Set(value)
            _automaticBatches.BatchSourceEmail = value
        End Set
    End Property
    Public Property ExecutionDateTime
        Get
            If _automatedBatchResults IsNot Nothing Then Return _automatedBatchResults.ExecutionDateTime
            Return Nothing
        End Get
        Set(value)
            If _automatedBatchResults IsNot Nothing Then _automatedBatchResults.ExecutionDateTime = value
        End Set
    End Property
    Public Property TotalSuccesful
        Get
            If _automatedBatchResults IsNot Nothing Then Return _automatedBatchResults.TotalSuccesful
            Return Nothing
        End Get
        Set(value)
            If _automatedBatchResults IsNot Nothing Then _automatedBatchResults.TotalSuccesful = value
        End Set
    End Property
    Public Property TotalItems
        Get
            If _automatedBatchResults IsNot Nothing Then Return _automatedBatchResults.TotalItems
            Return Nothing
        End Get
        Set(value)
            If _automatedBatchResults IsNot Nothing Then _automatedBatchResults.TotalItems = value
        End Set
    End Property
    Public Property TotalFail
        Get
            If _automatedBatchResults IsNot Nothing Then Return _automatedBatchResults.TotalFail
            Return Nothing
        End Get
        Set(value)
            If _automatedBatchResults IsNot Nothing Then _automatedBatchResults.TotalFail = value
        End Set
    End Property

End Class
于 2013-02-27T11:58:39.043 回答
0

发现出了什么问题。我没有在 GridView 的属性上设置 DataPropertyName。将 DataPropertyName 设置为它工作的数据库字段的相同名称后。我还在 EF 数据模型中创建了 AutomatedBatch (1) 与 AutomatedBatchResult (many) 之间的关联。

于 2013-02-27T16:01:41.370 回答