|
-
Mar 31st, 2004, 12:59 PM
#1
Thread Starter
Hyperactive Member
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)

-
Mar 31st, 2004, 01:09 PM
#2
Thread Starter
Hyperactive Member
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)

-
Apr 1st, 2004, 01:53 PM
#3
Frenzied Member
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:
Private Sub FillTree()
Dim sqlCon As New SqlConnection("server=(local);database=Reading;trusted_connection=true;")
' Level 0 Node(s) or the root
Dim sqlLevel0 As New SqlDataAdapter("SELECT * FROM [Menu] WHERE [PARENT_ID] = '0'", sqlCon)
Dim dataLevel0 As New DataTable
sqlLevel0.Fill(dataLevel0)
For i As Integer = 0 To dataLevel0.Rows.Count - 1
TreeView1.Nodes.Add(dataLevel0.Rows(i).Item("DESCR").ToString())
' Level 1 Nodes
Dim sqlLevel1 As New SqlDataAdapter("SELECT * FROM [Menu] WHERE [PARENT_ID] = '" + dataLevel0.Rows(i).Item("CAT_ID").ToString() + "'", sqlCon)
Dim dataLevel1 As New DataTable
sqlLevel1.Fill(dataLevel1)
For j As Integer = 0 To dataLevel1.Rows.Count - 1
TreeView1.Nodes(i).Nodes.Add(dataLevel1.Rows(j).Item("DESCR").ToString())
' Level 2 Nodes
Dim sqlLevel2 As New SqlDataAdapter("SELECT * FROM [Menu] WHERE [PARENT_ID] = '" + dataLevel1.Rows(j).Item("CAT_ID").ToString() + "'", sqlCon)
Dim dataLevel2 As New DataTable
sqlLevel2.Fill(dataLevel2)
For k As Integer = 0 To dataLevel2.Rows.Count - 1
TreeView1.Nodes(i).Nodes(j).Nodes.Add(dataLevel2.Rows(k).Item("DESCR").ToString())
' Level 3 Nodes
Dim sqlLevel3 As New SqlDataAdapter("SELECT * FROM [Menu] WHERE [PARENT_ID] = '" + dataLevel2.Rows(k).Item("CAT_ID").ToString() + "'", sqlCon)
Dim dataLevel3 As New DataTable
sqlLevel3.Fill(dataLevel3)
For m As Integer = 0 To dataLevel3.Rows.Count - 1
TreeView1.Nodes(i).Nodes(j).Nodes(k).Nodes.Add(dataLevel3.Rows(m).Item("DESCR").ToString())
Next
Next
Next
Next
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|