Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Windows FormTreeView from Datatable

Status
Not open for further replies.

RJL1

Technical User
Oct 3, 2002
228
US
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:
            DataTable dt = new DataTable();
            SqlCommand cmd = new SqlCommand();

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

            cs.Open();
            cmd.Connection = cs;
            cmd.ExecuteNonQuery();
            cs.Close();

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

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

Any assistance is appreciated.
RJL
 
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:
Code:
[COLOR=#0000FF]using[/color] System.Windows.Forms;
 
[COLOR=#0000FF]namespace[/color] TreeViewTest
{
    [COLOR=#0000FF]public[/color] [COLOR=#0000FF]partial[/color] [COLOR=#0000FF]class[/color] [COLOR=#2B91AF]Form1[/color] : [COLOR=#2B91AF]Form[/color]
    {
        [COLOR=#0000FF]public[/color] Form1()
        {
            InitializeComponent();
 
            [COLOR=#2B91AF]Root[/color] root = [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]Root[/color]([COLOR=#A31515]"Management Reports"[/color]);
 
            [COLOR=#2B91AF]Folder[/color] inbound = [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]Folder[/color]([COLOR=#A31515]"Inbound"[/color]);
            [COLOR=#2B91AF]Folder[/color] labor = [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]Folder[/color]([COLOR=#A31515]"Labor"[/color]);
            [COLOR=#2B91AF]Folder[/color] outbound = [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]Folder[/color]([COLOR=#A31515]"Outbound"[/color]);
            [COLOR=#2B91AF]Folder[/color] packaging = [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]Folder[/color]([COLOR=#A31515]"Packaging"[/color]);
            [COLOR=#2B91AF]Folder[/color] stats = [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]Folder[/color]([COLOR=#A31515]"Statistics"[/color]);
 
            [COLOR=#2B91AF]Report[/color] inOne = [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]Report[/color]([COLOR=#A31515]"Inbound Report 1"[/color]);
            [COLOR=#2B91AF]Report[/color] inTwo = [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]Report[/color]([COLOR=#A31515]"Inbound Report 2"[/color]);
            inbound.AddReport(inOne);
            inbound.AddReport(inTwo);
 
            [COLOR=#2B91AF]Report[/color] labOne = [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]Report[/color]([COLOR=#A31515]"Labor Report 1"[/color]);
            [COLOR=#2B91AF]Report[/color] labTwo = [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]Report[/color]([COLOR=#A31515]"Labor Report 2"[/color]);
            [COLOR=#2B91AF]Report[/color] labThree = [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]Report[/color]([COLOR=#A31515]"Labor Report 3"[/color]);
            labor.AddReport(labOne);
            labor.AddReport(labTwo);
            labor.AddReport(labThree);
 
            [COLOR=#2B91AF]Report[/color] outOne = [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]Report[/color]([COLOR=#A31515]"Outbound Report 1"[/color]);
            outbound.AddReport(outOne);
 
            [COLOR=#2B91AF]Report[/color] packOne = [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]Report[/color]([COLOR=#A31515]"Packaging Report 1"[/color]);
            packaging.AddReport(packOne);
 
            [COLOR=#2B91AF]Report[/color] statOne = [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]Report[/color]([COLOR=#A31515]"Statistics Report 1"[/color]);
            [COLOR=#2B91AF]Report[/color] statTwo = [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]Report[/color]([COLOR=#A31515]"Statistics Report 2"[/color]);
            stats.AddReport(statOne);
            stats.AddReport(statTwo);
 
            root.AddFolder(inbound);
            root.AddFolder(labor);
            root.AddFolder(outbound);
            root.AddFolder(packaging);
            root.AddFolder(stats);
 
            treeView1.BeginUpdate();
            treeView1.Nodes.Add([COLOR=#0000FF]new[/color] [COLOR=#2B91AF]TreeNode[/color](root.Name));
            [COLOR=#0000FF]for[/color] ([COLOR=#0000FF]int[/color] i = 0; i < root.Folders.Count; i++)
            {
                treeView1.Nodes[0].Nodes.Add([COLOR=#0000FF]new[/color] [COLOR=#2B91AF]TreeNode[/color](root.Folders[i].Name));
                [COLOR=#0000FF]for[/color] ([COLOR=#0000FF]int[/color] j = 0; j < root.Folders[i].Reports.Count; j++)
                {
                    treeView1.Nodes[0].Nodes[i].Nodes.Add([COLOR=#0000FF]new[/color] [COLOR=#2B91AF]TreeNode[/color](root.Folders[i].Reports[j].Name));
                }
            }
            treeView1.EndUpdate();
        }
    }
}
 
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top