DataBox
DataBox

Reputation: 17

WPF - Populate TreeView From SQL

I have a list of homes in a database that I am attempting to populate a TreeView from and I can not seem to grasp how to do it. Coming from win forms this was pretty simple and straight forward. However, this conversion does not seem to be as easy to understand.

In Win Forms I would query the DB, return all the rows into a datatable, then dump that datatable into the Tree. While dumping I would also match the ID of the row to call upon later.

Database

ID - House - Rooms - Bathrooms - Fence - Status
1   House 1    3         2         Y       FS
2   House 2    2         1         N       FS
3   House 3    1         1         Y        S
4   House 4    4         3         Y        S

Desired Tree View

Old Win Forms Code

' Add Homes to tree
    Dim houseList As DataTable = DBQuery()
    For Each home As DataRow In houseList.Rows

        Dim MyNode() As TreeNode
        MyNode = TreeView1.Nodes.Find("Homes", True)
        MyNode(0).Nodes.Add(home.Item("ID"), home.Item("House")).Nodes.Add(home.Item("Rooms"))

    Next home

Unfortunately everything I've found was in C# and not VB. Attempting to convert the examples were pretty hard to follow to continue to mold.

I've attempted to use follow these two links, but to no avail I was not able to succeed. https://www.codeproject.com/Articles/124644/Basic-Understanding-of-Tree-View-in-WPF

Thanks for any help or pointers provided!

Upvotes: 1

Views: 1308

Answers (1)

Ryan Thomas
Ryan Thomas

Reputation: 2012

Would appear you are just not aware of the new properties in WPF.

TreeViews now use Items instead of nodes. The Header property is the text.

Use my image below to help, it's fairly simple so can be expanded, any further questions let me know.

My Working Code

Upvotes: 2

Related Questions