Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Windows FormTreeView from Datatable

Windows FormTreeView from Datatable

Windows FormTreeView from Datatable

I have been trying for a while to get his to work. I have a table that list the root folder, sub-folders and reports in a database. I am trying to bind this to a TreeView in a windows form

My raw data looks like this

CODE -->

Type	IDParent	NameParent	        ReportName

Root	NULL	        Management Reports	NULL
Folder	0	        Inbound	                NULL	
Report	1	        NULL	                Inbound Report 1	
Report	1	        NULL	                Inbound Report 2	
Folder	0	        Labor	                NULL
Report	1	        NULL	                Labor Report 1	
Report	1	        NULL	                Labor Report 2
Report	1	        NULL	                Labor Report 3	
Folder	0	        Outbound	        NULL
Report	1	        NULL	                Outbound Report 1
Folder	0	        Packaging	        NULL
Report	1	        NULL	                Packaging Report 1
Folder	0	        Statistics	        NULL
Report	1	        NULL	                Statistics Report 1
Report	1	        NULL	                Statistics Report 1 

This is what I am trying to end up with.

CODE -->

+ Management Reports
    + Inbound
         Inbound Report 1	
         Inbound Report 2	
    + labor
         Labor Report 1	
         Labor Report 2
         Labor Report 3	
    + Outbound
         Outbound Report 1
    + Packaging
         Packaging Report 1
    + Statistics
         Statistics Report 1
         Statistics Report 1 

I need this to be based on what the query returns from the DB. I got the connection and dt portion build just hang a hard time getting this to go from datatable to treeview

CODE --> c#

DataTable dt = new DataTable();
            SqlCommand cmd = new SqlCommand();

            cmd.CommandText = "SSRS_TREE_VIEW_LIST";
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Connection = cs;

            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = cmd;

Also I can change the qury to how ever it should be to facilitate the treeview.

Any assistance is appreciated.

RE: Windows FormTreeView from Datatable

You're going to run into a few issues, the main one being that you don't seem to have any kind of unique identifier to associate folders to root objects, or reports to folder objects. Every item is going to need a unique sequence number, even the root (if you end up having multiples). Then each folder will have their parent ID set to the root's unique ID. Each report will have their parent set to the folder's unique sequence ID.

The tree view, as far as I can tell (I didn't dig deep mind) doesn't seem to have the most intelligent of controls out there. Here is a code example, and here is a snip of what that produces: https://onedrive.live.com/redir?resid=BB6F29C6F1F2...

CODE --> C#

using System.Windows.Forms;
namespace TreeViewTest
    public partial class Form1 : Form
        public Form1()
            Root root = new Root("Management Reports");
            Folder inbound = new Folder("Inbound");
            Folder labor = new Folder("Labor");
            Folder outbound = new Folder("Outbound");
            Folder packaging = new Folder("Packaging");
            Folder stats = new Folder("Statistics");
            Report inOne = new Report("Inbound Report 1");
            Report inTwo = new Report("Inbound Report 2");
            Report labOne = new Report("Labor Report 1");
            Report labTwo = new Report("Labor Report 2");
            Report labThree = new Report("Labor Report 3");
            Report outOne = new Report("Outbound Report 1");
            Report packOne = new Report("Packaging Report 1");
            Report statOne = new Report("Statistics Report 1");
            Report statTwo = new Report("Statistics Report 2");
            treeView1.Nodes.Add(new TreeNode(root.Name));
            for (int i = 0; i < root.Folders.Count; i++)
                treeView1.Nodes[0].Nodes.Add(new TreeNode(root.Folders[i].Name));
                for (int j = 0; j < root.Folders[i].Reports.Count; j++)
                    treeView1.Nodes[0].Nodes[i].Nodes.Add(new TreeNode(root.Folders[i].Reports[j].Name));

RE: Windows FormTreeView from Datatable

So, you can hook into an event that will fire when you add a node. Basically create an event that sees when you add the root node, and then add the folder nodes. When those get fired, you add the report nodes. So on and so on. Its a bit more flexible that way.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close