User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C# section within the Software Development category of DaniWeb, a massive community of 403,396 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,662 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C# advertiser: Programming Forums
Views: 3192 | Replies: 2
Reply
Join Date: Apr 2008
Posts: 3
Reputation: samudebr is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
samudebr samudebr is offline Offline
Newbie Poster

How to populate a TreeView

  #1  
Apr 10th, 2008
Can some one help with the code how I am going to populate a treeview. In my database i have a talbe with the following coloums: ID, ParentId, NodeText. Now i have the data in class named Cateogry and in it i have all the above colums. Then I created a list of type category and in it i have all recoreds. Now I want to view them in a tree vew. Can someone help me with the code?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2006
Location: Bonners Ferry, ID
Posts: 279
Reputation: JerryShaw is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 38
JerryShaw JerryShaw is offline Offline
Posting Whiz in Training

Re: How to populate a TreeView

  #2  
Apr 10th, 2008
Here is a simplified way of doing it.
Typically, If using a database, I would avoid using the category class all together
but that is not what you were asking for.

Anyway, All this form has is a TreeView component and an untyped dataset. The onLoad takes off and loads a
dataset from code (just to avoid having to create a table, and all that connection stuff).
Then I load the List<Category> with the data.
Finally build the tree using a recursive method named getChildren.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace treeTest
{
    public partial class Form1 : Form
    {
        private List<Category> items = new List<Category>();
        public Form1()
        {
            InitializeComponent();
        }
        /*
         * dataSet1 has a table with 3 columns
         * col#1=ID (int)
         * col#2=ParentID (int)
         * col#3=NodeText (string)
         */ 
        private void Form1_Load(object sender, EventArgs e)
        {
            // Load up a dataset with values - usually from a database, but
            // here I can show the data without using a real database.
            dataSet1.Tables[0].Rows.Add(1, 0, "First Node");
            dataSet1.Tables[0].Rows.Add(3, 1, "Node 3.1");
            dataSet1.Tables[0].Rows.Add(4, 1, "Node 4.1");
            dataSet1.Tables[0].Rows.Add(5, 0, "Node 5.0");
            dataSet1.Tables[0].Rows.Add(6, 5, "Node 6.5");
            dataSet1.Tables[0].Rows.Add(7, 5, "Node 7.5");
            dataSet1.Tables[0].Rows.Add(8, 5, "Node 8.5");
            dataSet1.Tables[0].Rows.Add(9, 5, "Node 9.5");
            dataSet1.Tables[0].Rows.Add(10, 6, "Node 10.6");
            dataSet1.Tables[0].Rows.Add(11, 6, "Node 11.6");
            dataSet1.Tables[0].Rows.Add(12, 11, "Node 12.11");
            dataSet1.Tables[0].Rows.Add(13, 11, "Node 13.11");
            dataSet1.Tables[0].Rows.Add(14, 11, "Node 14.11");
            dataSet1.Tables[0].Rows.Add(15, 11, "Node 15.11");

            // Populate the items array of Catagory from the dataset
            foreach(DataRow dr in dataSet1.Tables[0].Rows)
                items.Add( new Category( (int)dr[0], (int)dr[1], (string)dr[2] ));
            
            // Build the treeview from the catagory list
            buildtree();

        }

        private void buildtree()
        {
            treeView1.Nodes.Clear();    // Clear any existing items
            treeView1.BeginUpdate();    // prevent overhead and flicker
            LoadBaseNodes();            // load all the lowest tree nodes
            treeView1.EndUpdate();      // re-enable the tree
            treeView1.Refresh();        // refresh the treeview display
        }

        private void LoadBaseNodes()
        {
            int baseParent = 0;                 // Find the lowest root category parent value
            TreeNode node;
            foreach (Category cat in items)
            {
                if (cat.ParentID < baseParent)
                    baseParent = cat.ParentID;
            }
            foreach (Category cat in items)         // iterate through the categories
            {
                if (cat.ParentID == baseParent)     // found a matching root item
                {
                    node = treeView1.Nodes.Add(cat.NodeText); // add it to the tree
                    node.Tag = cat;                 // send the category into the tag for future processing
                    getChildren(node);              // load all the children of this node
                }
            }
        }

        // recursive tree loader. Passes back in a node to retireve its childre
        // until there are no more children for this node.
        private void getChildren(TreeNode node)
        {
            TreeNode Node = null;
            Category nodeCat = (Category)node.Tag;  // get the category for this node
            foreach (Category cat in items)         // locate all children of this category
            {
                if (cat.ParentID == nodeCat.ID)     // found a child
                {
                    Node = node.Nodes.Add(cat.NodeText);    // add the child
                    Node.Tag = cat;                         // set its tag to its category
                    getChildren(Node);                      // find this child's children
                }
            }
        }
    }

    class Category 
    {
        public int ID;
        public int ParentID;
        public string NodeText;
        public Category(int ID, int ParentID, string NodeText)
        {
            this.ID = ID;
            this.ParentID = ParentID;
            this.NodeText = NodeText;
        }
        public override string ToString()
        {
            return this.NodeText;
        }
    }
}

If this solves your problem, please mark it as solved, So I get credit for the work,
Thanks,
Jerry
Last edited by JerryShaw : Apr 10th, 2008 at 11:07 pm.
Reply With Quote  
Join Date: May 2008
Posts: 3
Reputation: yaninea is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
yaninea yaninea is offline Offline
Newbie Poster

Re: How to populate a TreeView

  #3  
May 8th, 2008
Hi, I have a similar situation only I don“t want to do it trough a class, how can I make it so the treeview will load up directly from the db?
The fields on my db are basically the same as posted from samudebr
Thanks very much, I will really appreciate any help
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C# Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the C# Forum

All times are GMT -4. The time now is 9:38 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC