• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 416
  • Last Modified:

How to populate a TreeView control from database

I have a VS2010 C# Windows application, and I want to add a TreeView control to the form. I want to populate the TreeView control with data from tables within my SQL Server 2008 database.For example, my TreeView will have three nodes: Projects, Deliverables, and Times. Under each of these nodes, I want to pull data from the database, and add the data to each node. For Projects, the data will be pulled from "SELECT * FROM dbo.Projects", for Deliverables, the data will be pulled from "SELECT * FROM dbo.Tasks", and for Times, the data will be pulled from "SELECT * FROM dbo.WeekEndingDates". How would I go about doing? Another question I have is: once these nodes are populated, is there a way to change them upon user selection? For example, if a user selects "Project 1" under the Projects node, can the Deliverables node be repopulated with all the data from the database that is associated with "Project 1"? Example: User selects "Project 1" from Projects node, and then the Deliverables node is populated with "SELECT * FROM dbo.Tasks WHERE Project = 'Project 1'".
1 Solution
Bob LearnedCommented:
If you don't know about SQL Server Common Table Expressions (CTEs), for hierarchical data, take a look:

Recursive Queries Using Common Table Expressions

You can get all the data in an ordered list, and build the tree from the table.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now