Search code examples
vb.netwinformsobservablecollectionobjectlistview

DataListView with DataSource of ObservableCollection does not display newly added objects


  1. Created form with a single button and a single DataListView.

  2. Added this code to form:

    Dim dummies As New ObservableCollection(Of Dummy)

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        dlvDummies.DataSource = dummies
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        dummies.Add(New Dummy(Now.ToLongTimeString()))
    End Sub

    Class Dummy
        Public Property X As String
        Sub New(x As String)
            Me.X = x
        End Sub
    End Class
  1. Ran the app, pressed the button. In the debugger, I can visibly see a new entry added to the ObservableCollection and correspondingly to the DataListView .Objects:

enter image description here

  1. Item never appears in the GUI (Yes, the DataListView's sole column is already set up with the aspect name "X" at design time.)

enter image description here

This is actually a bare bones reproduction of the problem I'm having in a larger app. The answer given in https://stackoverflow.com/a/30157854/2112855 implies that this should "just work". Is this a defect, or user error on my part?


Solution

  • Replacing ObservableCollection(Of foo) with BindingList(Of foo) fixed, or rather worked around this.

    I think, based on the discussion seen at https://sourceforge.net/p/objectlistview/discussion/812923/thread/ab0d6208/ that the accepted answer in https://stackoverflow.com/a/30157854/2112855 is faulty and should probably be edited.