Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. Problem in populating a treeview from database

Problem in populating a treeview from database

Scheduled Pinned Locked Moved ASP.NET
databasedata-structureshelpcareer
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rinku soni 23
    wrote on last edited by
    #1

    Hellon every one.I have a table as follow; Node_Id Node_Name Parent_Id 1 Classified 0 2 Job 1 3 Matrimony 1 4 Property 1 5 ChildofJob1 2 6 Childofjob2 2 i am using following code,but its not giving me proper structure. My logic is,i add the node in to treeview at the same time i also check is this node present into Arraylist or not,if yes then i don't add that node if no then add the node to treeview as well as arraylist also.This procedure used for each and every node of tree. // For database operation OdbcConnection con = CodeClass.GetConnetion(); OdbcDataAdapter adapter1 = new OdbcDataAdapter("select * from AdminAddNewItem ", con); OdbcDataAdapter adapter2 = new OdbcDataAdapter("select * from AdminAddNewItem ", con); DataSet ds = new DataSet(); adapter1.Fill(ds, "Parent"); adapter2.Fill(ds, "Child"); ds.Relations.Add("relation", ds.Tables["Parent"].Columns["Node_Id"], ds.Tables["Child"].Columns["Parent_Id"]); // ======================================================================================== //I am takeing a arraylist in which i am going to maintan all nodes ArrayList list = new ArrayList(); string listitem; int flage1 = 0; int flage2 = 0; //============================= foreach (DataRow Prow in ds.Tables["Parent"].Rows) { // code for add parent nodes TreeNode pnode = new TreeNode(Prow["Node_Name"].ToString(), Prow["Parent_Id"].ToString()); //code for chech whather the node already present or not for (int i = 0; i < list.Count; i++) { if (pnode.Text.ToString() == list[i].ToString ()) { flage1++; break; } } if (flage1 > 0) { flage1 = 0; Response.Write("Node already exist"); } else { TreeView1.Nodes.Add(pnode); list.Add(pnode.Text.ToString()); } //code for add Child nodes // if (Prow.GetChildRows("relation") != null) { foreach (DataRow Crow in Prow.GetChildRows("relation")) { TreeNode Childnode = new TreeNode(Crow["Node_Name"].ToString()); pnode.ChildNodes.Add(Childnode); list.Add(Childnode.Text .ToString ()); } } }

    A C N 3 Replies Last reply
    0
    • R rinku soni 23

      Hellon every one.I have a table as follow; Node_Id Node_Name Parent_Id 1 Classified 0 2 Job 1 3 Matrimony 1 4 Property 1 5 ChildofJob1 2 6 Childofjob2 2 i am using following code,but its not giving me proper structure. My logic is,i add the node in to treeview at the same time i also check is this node present into Arraylist or not,if yes then i don't add that node if no then add the node to treeview as well as arraylist also.This procedure used for each and every node of tree. // For database operation OdbcConnection con = CodeClass.GetConnetion(); OdbcDataAdapter adapter1 = new OdbcDataAdapter("select * from AdminAddNewItem ", con); OdbcDataAdapter adapter2 = new OdbcDataAdapter("select * from AdminAddNewItem ", con); DataSet ds = new DataSet(); adapter1.Fill(ds, "Parent"); adapter2.Fill(ds, "Child"); ds.Relations.Add("relation", ds.Tables["Parent"].Columns["Node_Id"], ds.Tables["Child"].Columns["Parent_Id"]); // ======================================================================================== //I am takeing a arraylist in which i am going to maintan all nodes ArrayList list = new ArrayList(); string listitem; int flage1 = 0; int flage2 = 0; //============================= foreach (DataRow Prow in ds.Tables["Parent"].Rows) { // code for add parent nodes TreeNode pnode = new TreeNode(Prow["Node_Name"].ToString(), Prow["Parent_Id"].ToString()); //code for chech whather the node already present or not for (int i = 0; i < list.Count; i++) { if (pnode.Text.ToString() == list[i].ToString ()) { flage1++; break; } } if (flage1 > 0) { flage1 = 0; Response.Write("Node already exist"); } else { TreeView1.Nodes.Add(pnode); list.Add(pnode.Text.ToString()); } //code for add Child nodes // if (Prow.GetChildRows("relation") != null) { foreach (DataRow Crow in Prow.GetChildRows("relation")) { TreeNode Childnode = new TreeNode(Crow["Node_Name"].ToString()); pnode.ChildNodes.Add(Childnode); list.Add(Childnode.Text .ToString ()); } } }

      A Offline
      A Offline
      Abhijit Jana
      wrote on last edited by
      #2

      Please post your code with proper format.

      Abhijit Jana | Codeproject MVP Web Site : abhijitjana.net Don't forget to click "Good Answer" on the post(s) that helped you.

      1 Reply Last reply
      0
      • R rinku soni 23

        Hellon every one.I have a table as follow; Node_Id Node_Name Parent_Id 1 Classified 0 2 Job 1 3 Matrimony 1 4 Property 1 5 ChildofJob1 2 6 Childofjob2 2 i am using following code,but its not giving me proper structure. My logic is,i add the node in to treeview at the same time i also check is this node present into Arraylist or not,if yes then i don't add that node if no then add the node to treeview as well as arraylist also.This procedure used for each and every node of tree. // For database operation OdbcConnection con = CodeClass.GetConnetion(); OdbcDataAdapter adapter1 = new OdbcDataAdapter("select * from AdminAddNewItem ", con); OdbcDataAdapter adapter2 = new OdbcDataAdapter("select * from AdminAddNewItem ", con); DataSet ds = new DataSet(); adapter1.Fill(ds, "Parent"); adapter2.Fill(ds, "Child"); ds.Relations.Add("relation", ds.Tables["Parent"].Columns["Node_Id"], ds.Tables["Child"].Columns["Parent_Id"]); // ======================================================================================== //I am takeing a arraylist in which i am going to maintan all nodes ArrayList list = new ArrayList(); string listitem; int flage1 = 0; int flage2 = 0; //============================= foreach (DataRow Prow in ds.Tables["Parent"].Rows) { // code for add parent nodes TreeNode pnode = new TreeNode(Prow["Node_Name"].ToString(), Prow["Parent_Id"].ToString()); //code for chech whather the node already present or not for (int i = 0; i < list.Count; i++) { if (pnode.Text.ToString() == list[i].ToString ()) { flage1++; break; } } if (flage1 > 0) { flage1 = 0; Response.Write("Node already exist"); } else { TreeView1.Nodes.Add(pnode); list.Add(pnode.Text.ToString()); } //code for add Child nodes // if (Prow.GetChildRows("relation") != null) { foreach (DataRow Crow in Prow.GetChildRows("relation")) { TreeNode Childnode = new TreeNode(Crow["Node_Name"].ToString()); pnode.ChildNodes.Add(Childnode); list.Add(Childnode.Text .ToString ()); } } }

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        If your code works at all, it defines two levels, not three. I would imagine you can't do this in a way where you just bind to a datasource, or expect a table to work out the details for you. Your code is hideously inefficient on many fronts. It looks to me like it will add every node at the top level, also.

        Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

        1 Reply Last reply
        0
        • R rinku soni 23

          Hellon every one.I have a table as follow; Node_Id Node_Name Parent_Id 1 Classified 0 2 Job 1 3 Matrimony 1 4 Property 1 5 ChildofJob1 2 6 Childofjob2 2 i am using following code,but its not giving me proper structure. My logic is,i add the node in to treeview at the same time i also check is this node present into Arraylist or not,if yes then i don't add that node if no then add the node to treeview as well as arraylist also.This procedure used for each and every node of tree. // For database operation OdbcConnection con = CodeClass.GetConnetion(); OdbcDataAdapter adapter1 = new OdbcDataAdapter("select * from AdminAddNewItem ", con); OdbcDataAdapter adapter2 = new OdbcDataAdapter("select * from AdminAddNewItem ", con); DataSet ds = new DataSet(); adapter1.Fill(ds, "Parent"); adapter2.Fill(ds, "Child"); ds.Relations.Add("relation", ds.Tables["Parent"].Columns["Node_Id"], ds.Tables["Child"].Columns["Parent_Id"]); // ======================================================================================== //I am takeing a arraylist in which i am going to maintan all nodes ArrayList list = new ArrayList(); string listitem; int flage1 = 0; int flage2 = 0; //============================= foreach (DataRow Prow in ds.Tables["Parent"].Rows) { // code for add parent nodes TreeNode pnode = new TreeNode(Prow["Node_Name"].ToString(), Prow["Parent_Id"].ToString()); //code for chech whather the node already present or not for (int i = 0; i < list.Count; i++) { if (pnode.Text.ToString() == list[i].ToString ()) { flage1++; break; } } if (flage1 > 0) { flage1 = 0; Response.Write("Node already exist"); } else { TreeView1.Nodes.Add(pnode); list.Add(pnode.Text.ToString()); } //code for add Child nodes // if (Prow.GetChildRows("relation") != null) { foreach (DataRow Crow in Prow.GetChildRows("relation")) { TreeNode Childnode = new TreeNode(Crow["Node_Name"].ToString()); pnode.ChildNodes.Add(Childnode); list.Add(Childnode.Text .ToString ()); } } }

          N Offline
          N Offline
          N a v a n e e t h
          wrote on last edited by
          #4

          A tree is a basic data structure and everyone should know to work with that. When you add a node, you need to find the parent of that node using the parent id. To do this, keep a reference to the root node and iterate recursively until you find the node with the parent id. New node should be added as a child of this node. Above method has a O(n) complexity where n is the total number of nodes in a tree. An efficient approach will be to keep each node in an associative container with node id as key. When adding each item, look for a TreeNode object in this container. Here is a working code.

          void PopulateTreeView(DataTable dt, TreeView tv)
          {
          Dictionary<int, TreeNode> nodes = new Dictionary<int, TreeNode>();
          TreeNode root = null;
          foreach (DataRow row in dt.Rows)
          {
          int parentId = int.Parse(row["Parent_Id"].ToString());
          int nodeId = int.Parse(row["Node_Id"].ToString());
          string nodeName = row["Node_Name"].ToString();

              TreeNode node;
              if (nodes.TryGetValue(parentId, out node))
              {
                  TreeNode newNode = new TreeNode(nodeName);
                  nodes.Add(nodeId, newNode);
                  node.ChildNodes.Add(newNode);
              }
              else
              {
                  node = new TreeNode(nodeName);
                  nodes.Add(nodeId, node);
                  node.ChildNodes.Add(node);
                  if (parentId == 0)
                      root = node;
              }
          }
          tv.Nodes.Add(root);
          

          }

          :)

          Navaneeth How to use google | Ask smart questions

          A 1 Reply Last reply
          0
          • N N a v a n e e t h

            A tree is a basic data structure and everyone should know to work with that. When you add a node, you need to find the parent of that node using the parent id. To do this, keep a reference to the root node and iterate recursively until you find the node with the parent id. New node should be added as a child of this node. Above method has a O(n) complexity where n is the total number of nodes in a tree. An efficient approach will be to keep each node in an associative container with node id as key. When adding each item, look for a TreeNode object in this container. Here is a working code.

            void PopulateTreeView(DataTable dt, TreeView tv)
            {
            Dictionary<int, TreeNode> nodes = new Dictionary<int, TreeNode>();
            TreeNode root = null;
            foreach (DataRow row in dt.Rows)
            {
            int parentId = int.Parse(row["Parent_Id"].ToString());
            int nodeId = int.Parse(row["Node_Id"].ToString());
            string nodeName = row["Node_Name"].ToString();

                TreeNode node;
                if (nodes.TryGetValue(parentId, out node))
                {
                    TreeNode newNode = new TreeNode(nodeName);
                    nodes.Add(nodeId, newNode);
                    node.ChildNodes.Add(newNode);
                }
                else
                {
                    node = new TreeNode(nodeName);
                    nodes.Add(nodeId, node);
                    node.ChildNodes.Add(node);
                    if (parentId == 0)
                        root = node;
                }
            }
            tv.Nodes.Add(root);
            

            }

            :)

            Navaneeth How to use google | Ask smart questions

            A Offline
            A Offline
            Abhishek Sur
            wrote on last edited by
            #5

            Good answer bro .. :thumbsup:

            Abhishek Sur


            My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB

            **Don't forget to click "Good Answer" if you like to.

            N 1 Reply Last reply
            0
            • A Abhishek Sur

              Good answer bro .. :thumbsup:

              Abhishek Sur


              My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB

              **Don't forget to click "Good Answer" if you like to.

              N Offline
              N Offline
              N a v a n e e t h
              wrote on last edited by
              #6

              Thanks mate.

              Navaneeth How to use google | Ask smart questions

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups