user3140169
user3140169

Reputation: 221

Bind listbox from a list of object inside an object

Here is a sample of 2 lists of Listing (tmp1 & tmp2) that are added to Data. Eventually this listbox will end up inside of a DataRepeater.

    Dim Data As New List(Of Test)

    Dim item As Test
    item = New Test       

    Dim tmp1 As New List(Of Listing)
    tmp1.Add(New Listing With {.ListingID = "004432", .Name = "Pizza Hut"})
    tmp1.Add(New Listing With {.ListingID = "024235", .Name = "Houston Pizza"})
    item.Listings.AddRange(tmp1)

    Data.Add(item)

    item = New Test

    Dim tmp2 As New List(Of Listing)
    tmp2.Add(New Listing With {.ListingID = "004432", .Name = "Pizza Hut"})
    tmp2.Add(New Listing With {.ListingID = "024235", .Name = "Houston Pizza"})
    item.Listings.AddRange(tmp2)

    Data.Add(item)

    Dim bs As New BindingSource
    bs.DataSource = Data

    ListBox1.DataSource = bs
    ListBox1.DisplayMember = "Listings.Listing.Name" ' I've tried many variations.

I've also tried:

    ListBox1.DataBindings.Add(New Binding("DisplayMember", bs, "Name"))
    ListBox1.DataBindings.Add(New Binding("DisplayMember", Data, "Name"))
    ListBox1.DataBindings.Add(New Binding("Items", bs, "Name"))
    ListBox1.DataBindings.Add(New Binding("Items", Data, "Name"))

I've not been able to Google a similar scenario for a solution. Any ideas?

Upvotes: 3

Views: 6566

Answers (2)

By copying your code, and of course adding these classes:

Public Class Test

    Public Sub New()
        Me.m_listings = New List(Of Listing)
    End Sub

    Public ReadOnly Property Listings() As List(Of Listing)
        Get
            Return Me.m_listings
        End Get
    End Property

    Private m_listings As List(Of Listing)

End Class

Public Class Listing

    Public Sub New()
        Me.m_listingID = String.Empty
        Me.m_name = String.Empty
    End Sub

    Public Property ListingID() As String
        Get
            Return Me.m_listingID
        End Get
        Set(value As String)
            Me.m_listingID = value
        End Set
    End Property

    Public Property Name() As String
        Get
            Return Me.m_name
        End Get
        Set(value As String)
            Me.m_name = value
        End Set
    End Property

    Private m_listingID As String
    Private m_name As String

End Class

I set the listbox like this:

Me.ListBox1.DataSource = bs
Me.ListBox1.DisplayMember = "Listings.Name"

And the result is this:

ListBox

Upvotes: 2

competent_tech
competent_tech

Reputation: 44921

The problem here is that you are attempting to use collections nested inside of another collection as the source for the ListBox, which won't work.

Technically, if you want to display this type of relationship, then you need a hierarchical visual control, such as a tree.

However, if you just want to display the inner collections (the Listings), then you need to extract them from their "parent" class (Test) and into a collection of their own, then bind this collection to the ListBox.

If you need to get back to the parent record in the future, then you will need to store a reference to it (primary key or some other unique identifier) within each Listing record.

Upvotes: 0

Related Questions