在VB中将对象列表绑定到datagridview

时间:2014-11-25 14:41:28

标签: vb.net list datagridview

我想在VB中将对象列表绑定到DataGridView。

首先使用datagrid.Rows.Add(...)来处理每个结果,但这会阻止程序。 我已经读过它可以通过使用.DataSource将对象列表绑定到DataGridView来解决。 但它不起作用,我的数据网格视图仍然是空的......

有什么问题?它是GUI代码中的东西(见下文)吗?

逻辑发生的地方:

Public queryList As New List(Of _study)

...

If _objResponse.Status = CurrentStatus.Pending Then
        Dim _newStudy As New _study

        ' Parse one study from the response (_objResponse.Dataset.[Get](Of String)(DicomTag.StudyDate))
        _newStudy._studyID = _objResponse.Dataset.[Get](Of String)(DicomTag.StudyInstanceUID)
        _newStudy._name =  _objResponse.Dataset.[Get](Of String)(DicomTag.AccessionNumber)

        ' Save study info for later use
        queryList.Add(_newStudy)            

ElseIf _objResponse.Status = DicomStatus.Success
        dgvResults.DataSource = queryList
        ...
Else
        MsgBox("Failed")
End If

这是_newStudy类:

Public Class _study

   Private _studyID As String
   Private _name As String

   Public Property _studyID() As String
     Get
        Return _studyID
      End Get
      Set(ByVal value As String)
        _studyID = value
      End Set
   End Property

   Public Property _name() As String
     Get
        Return _name
     End Get
     Set(ByVal value As String)
        _name = value
     End Set
   End Property
End Class

这是DataGridView()GUI的代码:

Private colName As System.Windows.Forms.DataGridViewTextBoxColumn
Private colID As System.Windows.Forms.DataGridViewTextBoxColumn
Private dgvResults As System.Windows.Forms.DataGridView

...

Me.dgvResults = New System.Windows.Forms.DataGridView()
Me.colID = New System.Windows.Forms.DataGridViewTextBoxColumn()
Me.colName = New System.Windows.Forms.DataGridViewTextBoxColumn()

Me.dgvResults.AllowUserToAddRows = false
Me.dgvResults.AllowUserToDeleteRows = false
Me.dgvResults.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill
Me.dgvResults.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
Me.dgvResults.Columns.AddRange(New System.Windows.Forms.DataGridViewColumn() {Me.colID, Me.colName})
Me.dgvResults.Location = New System.Drawing.Point(6, 19)
Me.dgvResults.Name = "dgvResults"
Me.dgvResults.ReadOnly = true
Me.dgvResults.Size = New System.Drawing.Size(540, 206)
Me.dgvResults.TabIndex = 2

'colID
Me.colID.HeaderText = "Study ID"
Me.colID.DataPropertyName = "_studyID"
Me.colID.Name = "colID"
Me.colID.ReadOnly = true

'colPatientName
Me.colName.HeaderText = "Name"
Me.colName.DataPropertyName = "_name"
Me.colName.Name = "_name"
Me.colName.ReadOnly = true

...

CType(Me.dgvResults,System.ComponentModel.ISupportInitialize).EndInit

我认为DataPropertyName对于获取对象和数据网格之间的链接是正确的,对吧......?

非常感谢!

1 个答案:

答案 0 :(得分:1)

List对象不会传输列表已更新或更改的任何信息。

为此,请尝试使用System.ComponentModel命名空间中的BindingList:

Public queryList As New BindingList(Of _study)