Hi Hutty -
I came across this code when I downloaded the Treeview control. It's using
the Pubs DB. It worked for me -
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not IsPostback then
Dim strConnectionString As String = [Your connection stuff]
Dim strSQLAuthors As String = "Select authors.au_id, authors.au_lname,
authors.au_fname from authors order by authors.au_lname, authors.au_fname,
authors.au_id"
Dim strSQLTitles as String = "Select titleauthor.au_id, titles.title
from titleauthor Inner Join titles on titleauthor.title_id = titles.title_id
Order by titles.title"
Dim nodeAuthor as TreeNode
Dim noteTitle as TreeNode
Dim cnn as SqlConnection
Dim cmAuthors as SqlDataAdapter
Dim cmTitles as SqlDataAdapter
Dim ds as DataSet
Dim rowAuthor as DataRow
Dim rowTitle as DataRow
cnn = New SqlConnection(strConnectionString)
ds = New DataSet()
cnn.Open()
cmAuthors = New SqlDataAdapter(strSQLAuthors, cnn)
cmAuthors.Fill(ds, "Authors")
cmTitles = New SqlDataAdapter(strSQLTitles, cnn)
cmTitles.Fill(ds, "Titles")
ds.Relations.Add("AuthorTitle", _
ds.Tables("Authors").Columns("au_id"), _
ds.Tables("Titles").Columns("au_id"))
For Each rowAuthor in ds.Tables("Authors").Rows
nodeauthor = New TreeNode()
nodeauthor.Text = rowauthor("au_lname") & "," _
& rowauthor("au_fname")
TreeView1.Nodes.Add(nodeAuthor)
Dim nodeTitle as TreeNode
For Each rowTitle In rowauthor.GetChildRows("AuthorTitle")
nodeTitle = New TreeNode()
nodeTitle.Text = RowTitle("Title")
nodeauthor.Nodes.Add(nodeTitle)
Next
Next
ds.Dispose()
cmauthors.Dispose
cmTitles.Dispose
cnn.Close
cnn.Dispose
End If
End Sub
Private Sub TreeView1_Expand(ByVal sender As Object, ByVal e As
Microsoft.Web.UI.WebControls.TreeViewClickEventArg s) Handles TreeView1.Expand
Dim strConnectionString As String = [Your connection stuff]
Dim strSQLTitles As String
Dim cnn as SqlConnection
Dim cmTitles As SqlDataAdapter
Dim ds As DataSet
Dim rowTitle as DataRow
Dim nodeTitle As TreeNode
Dim nodeAuthor As TreeNode
nodeAuthor = sender.nodes(e.Node.ToString)
If nodeAuthor.Nodes.Count = 0 Then
cnn = New SqlConnection(strConnectionString)
ds = New DataSet()
cnn.Open()
strSQLTitles = "Select titles.title from titles Inner Join titleauthor
On titles.title_id=titleauthor.title_id where titleauthor.au_id =
nodeAuthor.NodeData.ToString Order by titles.title"
cmTitles = New SqlDataAdapter(strSQLTitles, cnn)
cmTitles.Fill(ds, "Titles")
For Each rowTitle in ds.Tables("Titles").Rows
nodeTitle = New TreeNode()
nodeTitle.Text = rowTitle("Title")
nodeauthor.Nodes.Add(nodeTitle)
Next
ds.Dispose()
cmTitles.Dispose
cnn.Close
cnn.Dispose
End If
End Sub
"Hutty" wrote:
I have a table that has two columns, Parent and Child. I'm able to populate
a IEControls treeview with the Parent column. On the same row as the Parent
is the child in the Child colum. How do add the subnodes from the Child
column? Here's what I have so far. Thanks in advance.
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
Dim MyConnection As System.Data.OleDb.OleDbConnection
Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
Dim Tn As TreeNode
Dim CH As TreeNode
Dim DA As OleDb.OleDbDataAdapter
Dim ds As DataSet
Dim DR As DataRow
Dim DR2 As DataRow
'Create the Connection object and the DataSet object,
'and then open the connection.
MyConnection = New System.Data.OleDb.OleDbConnection( _
"provider=Microsoft.Jet.OLEDB.4.0; " & _
"data source=C:\Inetpub\wwwroot\intelis.MDB")
MyCommand = New System.Data.OleDb.OleDbDataAdapter( _
"select DISTINCT Parent, Child from Entity",
MyConnection)
ds = New DataSet
'Add an ENTITY table to the DataSet.
MyCommand.Fill(ds, "ENTITY")
'Populate the TreeView from the DataSet.
For Each DR In ds.Tables("ENTITY").Rows
Tn = New TreeNode
Tn.Text = DR("PARENT")
Tn.NodeData = DR("PARENT")
Tn.Expandable = ExpandableValue.CheckOnce
TreeView1.Nodes.Add(Tn)
Next
'Clean up.
ds.Dispose()
' DA.Dispose()
MyConnection.Close()
MyConnection.Dispose()
End If
End Sub
--
Hutty