[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 333
  • Last Modified:

How to build a tree Dynamically?

Hello
   How can i build a tree dynamically using VB window Application TreeView from the database information. I need to distinguish between root,parent and child.
0
Priyadarshni
Asked:
Priyadarshni
  • 6
  • 4
  • 2
  • +2
1 Solution
 
Bob LearnedCommented:
You have a tag that mentions Windows Forms application, and one of the zones is ASP.NET, so I am confused what you are looking for.
0
 
daveamourCommented:
Have a database table where one of the columns is ParentID.
If this is Null or zero then this node is the root.  If this refers to another row in the database table then this will be that nodes parent.
You should then load all your data into an in memory collection of some sort and then load this into your control using recursion.
Here is an exampel in C# but you can get the idea and then convert it to VB.net or just write your own having seen the general technique.
0
 
daveamourCommented:
Oops forgot yhe lnk - here it is:
http://www.daniweb.com/forums/thread118395.html
 
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
AxleWackCommented:
Here is some code I used. Check it out and see if it helps/If you understand it :

Private Sub PopulateRootLevel()
        Dim objConn As String = ConfigurationManager.AppSettings("Fleetcube.ConnectionString")
        Dim myConnection As New System.Data.SqlClient.SqlConnection(objConn)
        Dim objCommand As New SqlCommand("select id,name,parentBranchID, org_Organization_id, (select count(*) FROM org_Branch " _
          & "WHERE org_Organization_id = " & Session.Item("Admin_id") & ") childnodecount FROM org_Branch where parentBranchID = 0 AND org_organization_id =" & Session.Item("Admin_id"), _
          myConnection)
 
        Dim da As New SqlDataAdapter(objCommand)
        Dim dt As New DataTable()
        da.Fill(dt)
 
        PopulateNodes(dt, TreeView1.Nodes)
       
    End Sub
   
    'Nodes - Step 3
    Private Sub PopulateNodes(ByVal dt As DataTable, _
  ByVal nodes As TreeNodeCollection)
        For Each dr As DataRow In dt.Rows
            Dim tn As New TreeNode()
            tn.Text = dr("name").ToString()
            tn.Value = dr("id").ToString()
            nodes.Add(tn)
 
            'If node has child nodes, then enable on-demand populating
            tn.PopulateOnDemand = (CInt(dr("childnodecount")) > 0)
        Next
    End Sub
   
    'Sub levels - Step 3
    Private Sub PopulateSubLevel(ByVal parentid As Integer, ByVal parentNode As TreeNode)
        Dim objConn As String = ConfigurationManager.AppSettings("Fleetcube.ConnectionString")
        Dim myConnection As New System.Data.SqlClient.SqlConnection(objConn)
        Dim objCommand As New SqlCommand("select id,name,org_Organization_id,(select count(*) FROM org_Branch " _
          & "WHERE org_Organization_id = " & Session.Item("Admin_id") & ") childnodecount FROM org_Branch where parentBranchID=@parentBranchID", _
          myConnection)
        objCommand.Parameters.Add("@parentBranchID", SqlDbType.Int).Value = parentid
 
        Dim da As New SqlDataAdapter(objCommand)
        Dim dt As New DataTable()
        da.Fill(dt)      
        PopulateNodes(dt, parentNode.ChildNodes)
    End Sub
   

    'Step3 - Hierarchy Structure
    Protected Sub TreeView1_TreeNodePopulate(ByVal sender As Object, _
    ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs) Handles TreeView1.TreeNodePopulate
        PopulateSubLevel(CInt(e.Node.Value), e.Node)
    End Sub

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        If Not Page.IsPostBack Then
            PopulateRootLevel()
           
        End If
0
 
CodeCruiserCommented:
Following is the code copied from my working program. It uses LINQ for data but you can change it according to your needs.

    Private Sub LoadTree()
        ClientsTree.Nodes.Clear()
        Dim q = From cl In db.Clients Where cl.ParentID < 1
        ClientsTree.Nodes.Add("Clients")
        Dim TNode As TreeNode
        For Each c As Client In q
            If c.Children.Count = 0 And c.Contacts.Count = 0 Then
                TNode = New TreeNode
                If c.ClientType = "Parent" Or c.ClientType = "Subsidiary" Then
                    TNode.ImageIndex = 0
                    TNode.ImageIndex = 0
                ElseIf c.ClientType = "Department" Then
                    TNode.ImageIndex = 2
                    TNode.ImageIndex = 2
                End If
                TNode.Name = c.ClientID
                TNode.ContextMenuStrip = cmClientTree
                TNode.Text = c.Name & " (" & c.Children.Count + c.Contacts.Count & ")"
                TNode.Tag = c
                TNode.Nodes.Add("")
                ClientsTree.Nodes.Item(0).Nodes.Add(TNode)
            End If
        Next
        For Each c As Client In q
            If c.Children.Count > 0 Or c.Contacts.Count > 0 Then
                TNode = New TreeNode
                TNode.Name = c.ClientID
                TNode.ContextMenuStrip = cmClientTree
                TNode.Tag = c
                ClientsTree.Nodes.Item(0).Nodes.Add(TNode)
            End If
        Next
    End Sub
 
    Private Sub ClientsTree_BeforeExpand(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles ClientsTree.BeforeExpand
        If Not IsNothing(e.Node.Tag) Then
            Dim cl As Client = e.Node.Tag
            e.Node.Nodes.Clear()
            Dim TNode As TreeNode
            For Each ct As ClientContacts In cl.Contacts
                TNode = New TreeNode
                TNode.ImageIndex = 1
                TNode.SelectedImageIndex = 1
                TNode.Name = ct.ClientID
                TNode.Text = ct.Contact.Name & " (" & ct.Type.ContactType & ")" '& " (" & c.Children.Count + c.Contacts.Count & ")"
                TNode.Tag = ct
                TNode.ContextMenuStrip = cmClientTree
                e.Node.Nodes.Add(TNode)
            Next
        End If
    End Sub

Open in new window

0
 
CodeCruiserCommented:
I would say both comments from AxleWack (ID: 23679261) and me (ID: 23680811) could be considered answers. The difference with my comments is that it loads the tree on demand whenever a node is expanded while the AxleWack's code (according to my understanding) loads the whole tree including all subnodes altogather.
0
 
AxleWackCommented:
That is correct CodeCruiser. It works relatively nicely though. As it does not load on demand, I would believe it would load a bit slower, but I didnt see any performance issues to be too concerned about.
But I agree with you, both our comments could be concidered as asnwers.
0
 
PriyadarshniAuthor Commented:
hello..
I am building a hierarchical relationship management tree,in which when a user is logged into the system...he should get a tree which shows his supervisor and his subordinates...this is based on the  data given by him...like "who is your parent"  and "who is your child"..which is stored in database...how can i use your code to my application...i am a beginner to vb.net i was not able to relate your code to my application..
0
 
CodeCruiserCommented:
Then you should have simply asked rather than closing the question. The code i posted shows how to build a tree. You are now confused with how to retrieve values from database. The basic sketch of the code would remain same and you need to use code to retrieve values from database.
0
 
CodeCruiserCommented:
If you have loaded all your data, show me the code and i will rewrite my code.
0
 
PriyadarshniAuthor Commented:
Thanks for replying...      i have a database in which one of the field is relationship..now based on that information i need to build a tree..As i mentioned it contains one of the two questions.."who is your child" or "who is your parent"..Now based on the relationship data how can i build the tree?
0
 
CodeCruiserCommented:
The code for building the tree has already been posted. If you need help with the database then please post the full database schema and all the related information. The information you provide is not enough for me to understand the full application scenario.
0
 
PriyadarshniAuthor Commented:
Thanks again... In database i am storing the  job title and relationship data for each contact name...where in for each contact name i need to enter his job_title and his parent and children' s .Now  using this information i need to build a tree where in i get all my contacts in an hierarchical tree... how to build a hierarchical tree using this information...
0
 
CodeCruiserCommented:
You have to load all the tables in the database, go through each table and create nodes in the tree accordingly.
0
 
PriyadarshniAuthor Commented:
ok..thank you very much...
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 4
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now