Adding TreeNodes of TreeView Dynamically....

Last post 01-19-2009 8:46 AM by Chryzo. 4 replies.

Sort Posts:

  • Adding TreeNodes of TreeView Dynamically....

    01-28-2005, 9:49 PM
    • Member
      384 point Member
    • alecd
    • Member since 10-08-2003, 12:22 AM
    • Posts 147
    Hello everyone,

    I've reached a dead end right now trying to get my TreeNodes to load dynamically.

    I start by adding a base node as a starting point.


    Dim trnBase As New TreeNode
    trnBase.Text = "Search"
    tvw_Search.Nodes.Add(trnBase)


    At this point there are no problems. trnBase has been added to tvw_Search. And the indexOf property of trnBase has been set to 0 (which will be used later in this subroutine).

    Later on in this same subroutine I itterate through a DataTable containing my Nodes. Each itteration I check the ParentNodeIndex value. I then use that to get the ParentNode from the node collection of tvw_Search. Then I add the record as a node to the ParentNode.


    Dim intParentNodeIndex As Integer = drRow(4)

    Dim trnChildNode As New TreeNode
    trnChildNode.Text = drRow(7)

    Dim trnParentNode As New TreeNode
    trnParentNode = tvw_Search.Nodes.Item(intParentNodeIndex)
    trnParentNode.Nodes.Add(trnChildNode)


    Now this is where I have trouble. The problem lies in the IndexOf property of the child nodes. When I added trnBase (the first set of code) trnBase gets assigned "IndexOf = 0". However, the nodes that are added via the Database (the second set of code) are not assigned an IndexOf value.

    In other words, in my DataTable the first 5 records have "ParentIndexOf = 0". They are added to trnBase (indexOf = 0) no problem because trnBase was assigned an Index value of 0. The problem begins when I try to add nodes to these 5 new nodes. These 5 new nodes were never assigned an IndexOf value. Not only that, they aren't recognized in the tvw_Search nodes collection. When I do a "For each node in" tvw_Search, it only loops once for Node "trnBase", even though the first 5 records were added to "trnBase".

    The only difference that could make any difference in my code is the last line in each set:

    tvw_Search.Nodes.Add()

    trnBase.Nodes.Add()


    Is there something different I have to do as far as adding nodes to nodes to that of nodes to Trees?

    Thank you for any help.

    Alec
  • Re: Adding TreeNodes of TreeView Dynamically....

    08-08-2005, 4:51 AM
    • Member
      5 point Member
    • TomasF
    • Member since 08-05-2005, 11:53 AM
    • Posts 1

    Hello!

    I have the same problem, did you ever find a solution?

    This is my code:

    TreeView tree = new TreeView();

       TreeNode userTools = new TreeNode();
       userTools.Text="User Tools";
       tree.Nodes.Add(userTools);
       
       TreeNode userTool1 = new TreeNode();
       userTool1.Text="User Tool 1";
       tree.Nodes[tree.Nodes.IndexOf(userTools)].Nodes.Add(userTool1);

       TreeNode userTool2 = new TreeNode();
       userTool2.Text="User Tool 2";
       tree.Nodes[tree.Nodes.IndexOf(userTool1)].Nodes.Add(userTool2);

    this.Controls.Add(tree);


    I am able to add userTool1 to userTools, but when I want to add userTool2 to userTool1, I can't, because userTool1 has an index of -1(not assigned).

  • Re: Adding TreeNodes of TreeView Dynamically....

    08-10-2005, 11:46 AM
    • Member
      25 point Member
    • Dmerayo
    • Member since 08-09-2005, 2:09 PM
    • Posts 5
    Hi,

    You need to look for the node in the level you add it.

    Your example would be:

    TreeView tree = new TreeView();

       TreeNode userTools = new TreeNode();
       userTools.Text="User Tools";
       tree.Nodes.Add(userTools);
       
       TreeNode userTool1 = new TreeNode();
       userTool1.Text="User Tool 1"; 
       tree.Nodes[tree.Nodes.IndexOf(userTools)].Nodes.Add(userTool1);
       // tree.Nodes[0].Nodes.Add(userTool1); // This also works

       TreeNode userTool2 = new TreeNode();
       userTool2.Text="User Tool 2";
       tree.Nodes[tree.Nodes.IndexOf(userTools)].Nodes[userTools.Nodes.IndexOf(userTool1)].Nodes.Add(userTool2);
       // tree.Nodes[0].Nodes[0].Nodes.Add(userTool2); // This also works

    this.Controls.Add(tree);

    Remember the structure and hierarchy of the tree, in this case userTools belongs to the tree, but userTool1 belongs to userTools and userTool2 to userTool1
    I hope it helps

    Regards,
    Diego Merayo

  • Confused [8-)] Re: Adding TreeNodes of TreeView Dynamically....

    08-16-2005, 3:07 AM
    • Member
      105 point Member
    • prem_rajani
    • Member since 12-04-2004, 10:38 PM
    • Madison / Jackson - Mississippi
    • Posts 55

    Hi,

    I'm populating my treeview from SQL database. My table design is as follows:

    MenuId     |  MenuName      |  MenuUrl     |  MenuType  | MenuParent
    1                  Alert                     ----              root               0
    2                  Safety                   ----              document       1
    3                  Precaution             ----             folder              2
    4                  Caution                 ----             folder              3

    Each menu is linked to its parentid through Menuparent column. My problem is how to make the coding very dynamic. The treelevel could go upto 6-7 levels. The coding I have written is:

    TVMenu.ImageUrl = "../Images/folder.gif"
    TVMenu.SelectedImageUrl = "../Images/folderopen.gif"
    TVMenu.Visible =
    True
    da = New SqlDataAdapter("select * from menutrees where MenuType='root'", conn)
    da.Fill(dsRoot, "menutrees")
    For Each rowRoot In dsRoot.Tables("menutrees").Rows
       nodeRoot =
    New TreeNode
       nodeRoot.Text = rowRoot("MenuName")
       nodeRoot.Type = rowRoot("MenuType")
       nodeRoot.NavigateUrl = rowRoot("MenuUrl")
       nodeRoot.ImageUrl = "../Images/root.gif"
       nodeRoot.SelectedImageUrl = "../Images/root.gif"
       strid = rowRoot("MenuId")
       TVMenu.Nodes.Add(nodeRoot)
       da =
    New SqlDataAdapter("select * from menutrees where menuparent=" & strid & " ", conn)
       da.Fill(dsChild1, "menutrees")
       For Each rowChild1 In dsChild1.Tables("menutrees").Rows
          nodeChild1 =
    New TreeNode
          nodeChild1.Text = rowChild1("MenuName")
          nodeChild1.Type = rowChild1("MenuType")
          'nodeChild1.NavigateUrl = rowChild1("MenuUrl")
          If nodeChild1.Type = "document" Then
             nodeChild1.ImageUrl = "../Images/doc.gif"
             nodeChild1.SelectedImageUrl = "../Images/doc.gif"
          End If
          strid = rowChild1("MenuId")
          nodeRoot.Nodes.Add(nodeChild1)
          da =
    New SqlDataAdapter("select * from menutrees where menuparent=" & strid & " ", conn)
          da.Fill(dsChild2, "menutrees")
          For Each rowChild2 In dsChild2.Tables("menutrees").Rows
             ----
             ----
             ----

    So u guyz must have got an idea what I'm trying to do. I tried writing it as a procedure which will call itself. The problem is if any child record is present then datareader updates the dataset thereby old looping is lost and new loop begins. I hope you understood my problem. If anyone can give me some ideas how to go about I would appreciate.

    Prem
  • Re: Adding TreeNodes of TreeView Dynamically....

    01-19-2009, 8:46 AM
    • Member
      8 point Member
    • Chryzo
    • Member since 10-14-2008, 10:01 AM
    • Posts 27

    @ recursivity thingy 

    If you have a SQL Server (2005 +) there is a really nice clause to help you with recursivity problem, it is the WITH clause, see example D of: http://msdn.microsoft.com/en-us/library/ms175972.aspx

     As for the other problem, dunno.

    Personaly I am trying to add child nodes when client clicks on parent node, but even though it is adding the node, it ain't showing up on the client. (I use a debug label to count the number of childnodes added to the parent node and it shows the increase)

     Hope it helps in regards to recursivity

     

    Edit: Note that I do not want to add everything right of the bat ti minimize the response time and the load on the sql server

    Edit2: sorry for the necro, forgot to look up the date before posting

Page 1 of 1 (5 items)