How to create a treeview with a 3 level hierarchy??

Hello All experts........

I'm trying to populate a treeview from a database. i've already managed to do it. However, I need to create a treeview with a 3 level hierarchy in the following format:
-ELEMENT
 -SUBELEMENT
    SUBSUBELEMENT
    SUBSUBELEMENT
-ELEMENT
-ELEMENT
 +SUBELEMENT
.....

i've used the following methods to do the whole population. However, I keep getting errors.

==================================================================
using System;
using System.Drawing;
using System.Configuration;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace TMG
{
      public class Sites
      {
            private Guid identity;
            public Guid Identity
            {
                  get{return this.identity;}
            }
            
            private string address;
            public string Address
            {
                  get{return this.address;}
                  set{this.address = value;}
            }

            private string name;
            public string Name
            {
                  get{return this.name;}
                  set{this.name = value;}
            }

            public void Fill(DataRow dr){}
      }

      public class Groups
      {
            private Guid identity;
            public Guid Identity
            {
                  get{return this.identity;}
            }
            
            private string address;
            public string Address
            {
                  get{return this.address;}
                  set{this.address = value;}
            }
            
            private string name;
            public string Name
            {
                  get{return this.name;}
                  set{this.name = value;}
            }

            public void Fill(DataRow dr){}
      }
            
      public class Users
      {
            private Guid identity;
            public Guid Identity
            {
                  get{return this.identity;}
            }
            
            private string address;
            public string Address
            {
                  get{return this.address;}
                  set{this.address = value;}
            }

            private string name;
            public string Name
            {
                  get{return this.name;}
                  set{this.name = value;}
            }

            public void Fill(DataRow dr){}
      }

      public class Editor : System.Windows.Forms.Form
      {
            private System.Windows.Forms.TreeView treeView1;
            private System.Windows.Forms.Button button1;
            private System.Windows.Forms.TextBox textBox1;
            private System.ComponentModel.Container components = null;
            
            private DataTable dt;      
            private DataTable dt1;
            private DataTable dt2;
      
            public Editor()
            {
                  InitializeComponent();

                  string connstring = ConfigurationSettings.AppSettings["ConnectionString"];
                  this.dt = TMG.Data.DataAccess.GetResultTable("upDisplaySites", connstring);
                  this.dt1 = TMG.Data.DataAccess.GetResultTable("upDisplayGroups", connstring);
                  this.dt2 = TMG.Data.DataAccess.GetResultTable("upDisplayUsers", connstring);
                  
                  FillSites();
            }
      
            protected override void Dispose( bool disposing )
            {
                  if( disposing )
                  {
                        if (components != null)
                        {
                              components.Dispose();
                        }
                  }
                  base.Dispose( disposing );
            }

            #region Windows Form Designer generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                  this.treeView1 = new System.Windows.Forms.TreeView();
                  this.button1 = new System.Windows.Forms.Button();
                  this.textBox1 = new System.Windows.Forms.TextBox();
                  this.SuspendLayout();
                  //
                  // treeView1
                  //
                  this.treeView1.ImageIndex = -1;
                  this.treeView1.Location = new System.Drawing.Point(32, 8);
                  this.treeView1.Name = "treeView1";
                  this.treeView1.SelectedImageIndex = -1;
                  this.treeView1.Size = new System.Drawing.Size(288, 368);
                  this.treeView1.TabIndex = 1;
                  //
                  // button1
                  //
                  this.button1.Location = new System.Drawing.Point(112, 384);
                  this.button1.Name = "button1";
                  this.button1.Size = new System.Drawing.Size(96, 24);
                  this.button1.TabIndex = 2;
                  this.button1.Text = "button1";
                  //
                  // textBox1
                  //
                  this.textBox1.Location = new System.Drawing.Point(336, 16);
                  this.textBox1.Multiline = true;
                  this.textBox1.Name = "textBox1";
                  this.textBox1.ReadOnly = true;
                  this.textBox1.Size = new System.Drawing.Size(272, 360);
                  this.textBox1.TabIndex = 3;
                  this.textBox1.Text = "textBox1";
                  //
                  // Editor
                  //
                  this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                  this.ClientSize = new System.Drawing.Size(632, 413);
                  this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                                                              this.textBox1,
                                                                                                              this.button1,
                                                                                                              this.treeView1});
                  this.Name = "Editor";
                  this.Text = "Editor";
                  this.ResumeLayout(false);

            }
            #endregion
            
            public void RegenTree()
            {
                  treeView1.Nodes.Clear();  
                  RefreshTree();
            }
      
            public void RefreshTree()
            {
                  this.FillSites(treeView1.Nodes, dt.Rows);
            }
            
            private void FillSites(TreeNodeCollection nodes)
            {
                  for (int i=0; i < dt.Rows.Count; i++)
                  {
                        Sites nxtst = new Sites();
                        nxtst.Fill(dt.Rows[i]);
                        TreeNode exst = FindNode(nodes, nxtst.Identity);
                        if (exst == null)
                        {
                              exst = new TreeNode();
                              exst.Text = nxtst.Name;
                              exst.Tag = nxtst.Identity;
                        }
                        
                        FillGroups(exst.Nodes, nxtst);
                  }
            }
            
            private void FillGroups(TreeNodeCollection nodes, Sites parent)
            {
                  DataTable dt1 = TMG.Data.DataAccess.GetResultTable("upDisplayGroups", connstring);
                        
                  for (int i=0; i < dt1.Rows.Count; i++)
                  {
                        Groups nxtGrp = new Groups();
                        nxtGrp.Fill(dt1.Rows[i]);
                        if (nxtGrp.Parent == parent){continue;}
                        TreeNode exst = FindNode(nodes, nxtGrp.Identity);
                        if (exst == null)
                        {
                              exst = new TreeNode();
                              exst.Text = nxtGrp.Name;
                              exst.Tag = nxtGrp.Identity;
                        }
                        // update any other desired node properties
                        FillUsers(exst.Nodes, nxtGrp);
                  }
            }

            private void FillUsers(TreeNodeCollection nodes, Groups parent)
            {
                  DataTable dt2 = TMG.Data.DataAccess.GetResultTable("upDisplayUsers", connstring);
                  for (int i=0; i < dt2.Rows.Count; i++)
                  {
                        Users nxtusr = new Users();
                        nxtusr.Fill(dt2.Rows[i]);
                        if (nxtusr.Parent == parent){continue;}
                        TreeNode exst = FindNode(nodes, nxtusr.Identity);
                        if (exst == null)
                        {
                              exst = new TreeNode();
                              exst.Text = nxtusr.Name;
                              exst.Tag = nxtusr.Identity;
                        }
                  }
            }
            
            private TreeNode FindNode(TreeNodeCollection nodes, Guid iden)
            {
                  for (int i=0; i < nodes.Count; i++)
                  {
                        TreeNode nxt = nodes[i];
                        Guid tg = nxt.Tag as Guid;
                        if (tg == iden){return nxt;}
                  }
                  return null;
            }
      }
}
=================================================================================

The errors are as follows:
if (nxtGrp.Parent == parent)
('TMG.Groups' does not contain a definition for 'Parent')


Guid tg = nxt.Tag as Guid;
(The as operator must be used with a reference type ('System.Guid' is a value type))

and in addition, I don't know what parameters I should pass to the method FillSites() when called from the Main and RefreshTree?!!!

Any help would be very much appreciated as I need to complete this task by the end of the day or I am in a big trouble!!!

Thanks and looking forward to your replies.

Kam
KamalAliAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TonylmillerCommented:
First, I set up my dataset to have the following columns:

ID, NodeName, and ParentNodeId

Then, I just loop through and find the first root node (the root nodes are the ones which have no parent).  I add it to the root node collection.  Then I loop through and find the first node that has the root node as a parent and add it to the collection of child nodes for that root node.  Then I loop through and find the first node that has that child node as a parent, and so on.  Here is the code:

// Clear the TreeView each time the method is called.
trvReportList.Nodes.Clear();

// Instantiate the Reports DAL class
CDALReport myReports = new CDALReport();

// Instantiate the dataset and fill it with records
ReportTreeViewNodesDataSet dataSet = myReports.GetReportTreeViewNodes();
                  
// Load the dataset into a dataview for sorting
myDV = dataSet.Tables["ReportTreeViewNodes"].DefaultView;
                  
// Sort the dataview based on the name of the node.
myDV.Sort = "NodeName";

// Loop through the ReportTreeViewNodes table and look for the root nodes first.
for(int i=0; i<myDV.Count; i++)
      {
      // If it is a root node (null in the ParentId column)
      if(myDV[i]["ParentNodeId"].ToString()=="")      
            {
            // Add the root node to the nodes collection.
            trvReportList.Nodes.Add(new TreeNode(myDV[i]["NodeName"].ToString()));

            // Make sure the m_intParentNodeIndex starts at 0.
            m_intParentNodeIndex = 0;
                              
            // Now loop through the parent nodes (the child nodes of the root nodes) and see if any belong to the current root node.
            for(int j=0; j<myDV.Count; j++)
                  {
                        //If the ParentId of the current ParentNode [j] is the ID of the current root node
                        if(myDV[j]["ParentNodeId"].ToString() == myDV[i]["ID"].ToString())      
                        {
                              // Add the Parent node to the nodes collection for the Root node [m_intRootNodeIndex]
                              trvReportList.Nodes[m_intRootNodeIndex].Nodes.Add(new TreeNode(myDV[j]["NodeName"].ToString()));
                                          
                              // Make sure the m_intChildNodeIndex starts at 0.
                              m_intChildNodeIndex = 0;
                                          
                              // Now loop through the Child nodes (the child nodes of the Parent nodes) and see if any belong to the current parent node.
                              for(int k=0; k<myDV.Count; k++)
                                    {
                                    //If the ParentId of the current ChildNode [k] is the ID of the current Parent node
                                    if(myDV[k]["ParentNodeId"].ToString() == myDV[j]["ID"].ToString())      
                                          {
                                          // Add the Child node to the nodes collection for the Parent node
                                          trvReportList.Nodes[m_intRootNodeIndex].Nodes[m_intParentNodeIndex].Nodes.Add(new TreeNode(myDV[k]["NodeName"].ToString()));
                                                      
                                          //Make sure the m_intBabyNodeIndex starts at 0.
                                          m_intBabyNodeIndex = 0;

                                          //Now loop through the Baby nodes (the child nodes of the Child nodes) and see if any belong to the current Child node.
                                          for(int l=0; l<myDV.Count; l++)
                                          {
                                                // If the ParentId of the current BabyNode [l] is the ID of the current Child node
                                                if(myDV[l]["ParentNodeId"].ToString() == myDV[k]["ID"].ToString())      
                                                {
                                                      // Add the Baby node to the nodes collection for the Child node
                                                      trvReportList.Nodes[m_intRootNodeIndex].Nodes[m_intParentNodeIndex].Nodes[m_intChildNodeIndex].Nodes.Add(new TreeNode(myDV[l]["NodeName"].ToString()));
                                                                  
                                                      // Increment the Baby node index since this was a Baby node.
                                                      m_intBabyNodeIndex ++;
                                                }
                                          }
                                                      
                                          // Increment the Child node index since this was a Child node.
                                          m_intChildNodeIndex ++;
                                    }
                              }
                                          
                              // Increment the Parent node index since this was a Parent node.
                              m_intParentNodeIndex ++;
                        }
                  }
                              
                  // Increment the Root node index since this was a root node.
                  m_intRootNodeIndex ++;
            }
      }

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.