How to populate a TreeView control from database

Posted on 2011-10-31
Last Modified: 2012-05-12
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'".
Question by:jjliu4492
    1 Comment
    LVL 96

    Accepted Solution

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now