Results 1 to 3 of 3

Thread: Treeview Help

  1. #1

    Thread Starter
    Hyperactive Member kleptos's Avatar
    Join Date
    Aug 2001
    Location
    The Dark Carnival
    Posts
    346

    Unhappy Treeview Help

    I am totally stumped when it comes to the treeview control, but i really need to get this to work. I have the following database setup in Access and need to display it in a treeview.

    As you can see. the PARENT_ID matched the CAT_ID of its parent. Seems pretty straight forward to me. I just dont know how to build the treeview from the database and have it come out the correct way. Any ideas would be helpful.
    ..::[kleptos]::..
    • Database Administrator (MSSQL 2000)
    • Application Developer (C#)
    • Web Developer (ASP.NET)


  2. #2

    Thread Starter
    Hyperactive Member kleptos's Avatar
    Join Date
    Aug 2001
    Location
    The Dark Carnival
    Posts
    346
    If you want to see what it should look like, here is a picture. This is not dynamically created, its all hand written. I did this as a visual.
    ..::[kleptos]::..
    • Database Administrator (MSSQL 2000)
    • Application Developer (C#)
    • Web Developer (ASP.NET)


  3. #3
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    This is some VB.NET code I wrote to populate a TreeView from a SQL Server table.

    You'll have to convert the Connection and DataAdapters to OLE or ODBC. I tried to use your table structure as much as possible.
    VB Code:
    1. Private Sub FillTree()
    2.  
    3.         Dim sqlCon As New SqlConnection("server=(local);database=Reading;trusted_connection=true;")
    4.  
    5.         ' Level 0 Node(s) or the root
    6.         Dim sqlLevel0 As New SqlDataAdapter("SELECT * FROM [Menu] WHERE [PARENT_ID] = '0'", sqlCon)
    7.         Dim dataLevel0 As New DataTable
    8.  
    9.         sqlLevel0.Fill(dataLevel0)
    10.  
    11.         For i As Integer = 0 To dataLevel0.Rows.Count - 1
    12.             TreeView1.Nodes.Add(dataLevel0.Rows(i).Item("DESCR").ToString())
    13.  
    14.             ' Level 1 Nodes
    15.             Dim sqlLevel1 As New SqlDataAdapter("SELECT * FROM [Menu] WHERE [PARENT_ID] = '" + dataLevel0.Rows(i).Item("CAT_ID").ToString() + "'", sqlCon)
    16.             Dim dataLevel1 As New DataTable
    17.  
    18.             sqlLevel1.Fill(dataLevel1)
    19.  
    20.             For j As Integer = 0 To dataLevel1.Rows.Count - 1
    21.                 TreeView1.Nodes(i).Nodes.Add(dataLevel1.Rows(j).Item("DESCR").ToString())
    22.  
    23.                 ' Level 2 Nodes
    24.                 Dim sqlLevel2 As New SqlDataAdapter("SELECT * FROM [Menu] WHERE [PARENT_ID] = '" + dataLevel1.Rows(j).Item("CAT_ID").ToString() + "'", sqlCon)
    25.                 Dim dataLevel2 As New DataTable
    26.  
    27.                 sqlLevel2.Fill(dataLevel2)
    28.  
    29.                 For k As Integer = 0 To dataLevel2.Rows.Count - 1
    30.                     TreeView1.Nodes(i).Nodes(j).Nodes.Add(dataLevel2.Rows(k).Item("DESCR").ToString())
    31.  
    32.                     ' Level 3 Nodes
    33.                     Dim sqlLevel3 As New SqlDataAdapter("SELECT * FROM [Menu] WHERE [PARENT_ID] = '" + dataLevel2.Rows(k).Item("CAT_ID").ToString() + "'", sqlCon)
    34.                     Dim dataLevel3 As New DataTable
    35.  
    36.                     sqlLevel3.Fill(dataLevel3)
    37.  
    38.                     For m As Integer = 0 To dataLevel3.Rows.Count - 1
    39.                         TreeView1.Nodes(i).Nodes(j).Nodes(k).Nodes.Add(dataLevel3.Rows(m).Item("DESCR").ToString())
    40.                     Next
    41.                 Next
    42.             Next
    43.         Next
    44.  
    45.     End Sub
    There may be better ways to do it but it's the best I can think of.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width