VB.NET WPF正确填充datagrid

时间:2015-11-10 17:22:21

标签: wpf vb.net datagrid

我希望从表中提取数据,然后将其添加到列表中,然后使用该列表填充数据网格;然而,当我这样做时,我的网格上出现了错误的人口,尽管我知道我在循环中访问了正确的记录。

这是我的代码:

    Public Class Room

    Private _RoomNumber As String
    Public Property RoomNumber As String
        Get
            Return _RoomNumber
        End Get
        Set(ByVal value As String)
            _RoomNumber = value
        End Set
    End Property

    Private _PatientName As String
    Public Property PatientName As String
        Get
            Return _PatientName
        End Get
        Set(value As String)
            _PatientName = value
        End Set
    End Property

    Private _Physician As String
    Public Property Physician As String
        Get
            Return _Physician
        End Get
        Set(value As String)
            _Physician = value
        End Set
    End Property

    Private _Nurse As String
    Public Property Nurse As String
        Get
            Return _Nurse
        End Get
        Set(value As String)
            _Nurse = value
        End Set
    End Property

    Private _SchedTime As String
    Public Property SchedTime As String
        Get
            Return _SchedTime
        End Get
        Set(value As String)
            _SchedTime = value
        End Set
    End Property

    Private _ArrivalTime As String
    Public Property ArrivalTime As String
        Get
            Return _ArrivalTime
        End Get
        Set(value As String)
            _ArrivalTime = value
        End Set
    End Property

    Private _ES As String
    Public Property ES As String
        Get
            Return _ES
        End Get
        Set(value As String)
            _ES = value
        End Set
    End Property

End Class

Private Sub loadPage() Handles MyBase.Loaded
    Dim newList As New List(Of Room)
    newList = getData()
    MedDayDataGrid.ItemsSource = newList
End Sub

Private Function getData() As List(Of Room)
    Dim connection As New ODBCConnection() //Left this out for obv reasons
    connection.Open()
    Dim query As String = "SELECT visit.visit_name, visit.visit_admit_date, visit.visit_id, ip_visit_1.ipv1_room FROM visit INNER JOIN ip_visit_1 ON visit.visit_id=ip_visit_1.ipv1_num WHERE visit.visit_disch_date = '0001-01-01' AND ip_visit_1.ipv1_room LIKE 'MDH%' ORDER BY ip_visit_1.ipv1_room"
    Dim myCommand As New OdbcCommand(query, connection)

    Dim myRoom As New Room
    Dim myList As New List(Of Room)

    Dim myReader As OdbcDataReader = myCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
    While myReader.Read()
        myRoom.RoomNumber = myReader.GetString(3)
        myRoom.PatientName = myReader.GetString(0)
    End While

    myReader.Close()

    connection.Close()
    connection = Nothing

    Return myList
End Function

返回的数据显示在我的网格上20次添加到列表中的最后一次记录,而不是显示添加到getData()函数列表中的20个单独房间中的每一个。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

您需要为查询返回的每一行创建一个新的Room实例,并将其添加到List,例如

Private Function getData() As List(Of Room)
    Dim connection As New ODBCConnection() //Left this out for obv reasons
    connection.Open()
    Dim query As String = "SELECT visit.visit_name, visit.visit_admit_date, visit.visit_id, ip_visit_1.ipv1_room FROM visit INNER JOIN ip_visit_1 ON visit.visit_id=ip_visit_1.ipv1_num WHERE visit.visit_disch_date = '0001-01-01' AND ip_visit_1.ipv1_room LIKE 'MDH%' ORDER BY ip_visit_1.ipv1_room"
    Dim myCommand As New OdbcCommand(query, connection)

    Dim myList As New List(Of Room)

    Dim myReader As OdbcDataReader = myCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
    While myReader.Read()
        Dim myRoom As New Room
        myRoom.RoomNumber = myReader.GetString(3)
        myRoom.PatientName = myReader.GetString(0)
        myList.Add(myRoom)
    End While

    myReader.Close()

    connection.Close()
    connection = Nothing

    Return myList
End Function