Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I need some advice and guidance on how I should tackle this problem.

 

I have a treeview 'treeview1' that I want to list all the files that are stored within my database table 'docListTbl'. The problem is that these files might be stored within different treenodes, and I'm not sure how to build the treeview dynamically from my database table and to ensure that the files are displayed under the correct node.

 

What I will do is give you and example, but I am unsure how to acheive this, can anyone help???

 

If I have the following table stucture:

 

parent | child | file name

-----------------------

text files | new | filename

picture files | old | filename

text files | new1 | filename1

text files | new | filename2

 

so what I want it to give me is the following:

 

-- text files --
 |            |-- new --
 |            |         |-- filename
 |            |         |-- filename2
 |            |
 |            |-- new1--
 |                      |-- filename1
-- picture files --
                  |-- old --
                            |-- filename

 

 

If anyone could help give me an example code in vb.net that would be great??

 

Thanks

 

Simon

Posted

When you get the data from the table, ensure that you sort by parent, child, filename. Try the following:

 


   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

       Dim dt As New DataTable
       Dim iLp As Integer
       Dim parent As TreeNode
       Dim child As TreeNode
       Dim filename As TreeNode

       'da is a DataAdapter for the table
       da.Fill(dt)

       For iLp = 0 To dt.Rows.Count - 1

           parent = FindParent(dt.Rows(iLp).Item(0))

           If parent Is Nothing Then

               parent = TreeView1.Nodes.Add(dt.Rows(iLp).Item(0))

           End If

           child = FindChild(parent, dt.Rows(iLp).Item(1))

           If child Is Nothing Then
               child = parent.Nodes.Add(dt.Rows(iLp).Item(1))
           End If

           filename = FindChild(child, dt.Rows(iLp).Item(2))

           If filename Is Nothing Then
               filename = child.Nodes.Add(dt.Rows(iLp).Item(2))
           End If
       Next iLp

   End Sub

   Private Function FindParent(ByVal treeText As String) As TreeNode

       Dim node As TreeNode

       For Each node In TreeView1.Nodes
           If node.Text = treeText Then Return node
       Next node

       Return Nothing

   End Function

   Private Function FindChild(ByRef node As TreeNode, ByVal treeText As String) As TreeNode

       Dim child As TreeNode

       For Each child In node.Nodes
           If child.Text = treeText Then Return child
       Next child

       Return Nothing

   End Function

Here's what I'm up to.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...