francisMi
francisMi

Reputation: 935

Bind list of object to datagridview in VB

I want to bind a list of objects to a DataGridView in VB.

It worked first with using datagrid.Rows.Add(...) for each result, but that blocks the program. I've read it could be solved by binding a list of objects to the DataGridView with .DataSource. But it don't work, my datagridview remains empty...

What is wrong? Is it something in the GUI-code (see below)?

This where the logic happens:

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

Here is the _newStudy class:

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

And here is the code of the 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

I think the DataPropertyName is correct to get the link between the objects and the datagrid, right...?

Thanks a lot!

Upvotes: 0

Views: 5206

Answers (1)

LarsTech
LarsTech

Reputation: 81610

The List object doesn't transmit any information that the list has been updated or changed.

To do that, try using the BindingList from the System.ComponentModel namespace:

Public queryList As New BindingList(Of _study)

Upvotes: 1

Related Questions