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.EventArg s) Handles MyBase.Load
'Put user code to initialize the page here
If Not IsPostback then
Dim strConnectionSt ring As String = [Your connection stuff]
Dim strSQLAuthors As String = "Select authors.au_id, authors.au_lnam e,
authors.au_fnam e from authors order by authors.au_lnam e, authors.au_fnam e,
authors.au_id"
Dim strSQLTitles as String = "Select titleauthor.au_ id, titles.title
from titleauthor Inner Join titles on titleauthor.tit le_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(s trConnectionStr ing)
ds = New DataSet()
cnn.Open()
cmAuthors = New SqlDataAdapter( strSQLAuthors, cnn)
cmAuthors.Fill( ds, "Authors")
cmTitles = New SqlDataAdapter( strSQLTitles, cnn)
cmTitles.Fill(d s, "Titles")
ds.Relations.Ad d("AuthorTitle" , _
ds.Tables("Auth ors").Columns(" au_id"), _
ds.Tables("Titl es").Columns("a u_id"))
For Each rowAuthor in ds.Tables("Auth ors").Rows
nodeauthor = New TreeNode()
nodeauthor.Text = rowauthor("au_l name") & "," _
& rowauthor("au_f name")
TreeView1.Nodes .Add(nodeAuthor )
Dim nodeTitle as TreeNode
For Each rowTitle In rowauthor.GetCh ildRows("Author Title")
nodeTitle = New TreeNode()
nodeTitle.Text = RowTitle("Title ")
nodeauthor.Node s.Add(nodeTitle )
Next
Next
ds.Dispose()
cmauthors.Dispo se
cmTitles.Dispos e
cnn.Close
cnn.Dispose
End If
End Sub
Private Sub TreeView1_Expan d(ByVal sender As Object, ByVal e As
Microsoft.Web.U I.WebControls.T reeViewClickEve ntArgs) Handles TreeView1.Expan d
Dim strConnectionSt ring 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.Node s.Count = 0 Then
cnn = New SqlConnection(s trConnectionStr ing)
ds = New DataSet()
cnn.Open()
strSQLTitles = "Select titles.title from titles Inner Join titleauthor
On titles.title_id =titleauthor.ti tle_id where titleauthor.au_ id =
nodeAuthor.Node Data.ToString Order by titles.title"
cmTitles = New SqlDataAdapter( strSQLTitles, cnn)
cmTitles.Fill(d s, "Titles")
For Each rowTitle in ds.Tables("Titl es").Rows
nodeTitle = New TreeNode()
nodeTitle.Text = rowTitle("Title ")
nodeauthor.Node s.Add(nodeTitle )
Next
ds.Dispose()
cmTitles.Dispos e
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.EventArg s) Handles MyBase.Load
If Not IsPostBack Then
Dim MyConnection As System.Data.Ole Db.OleDbConnect ion
Dim MyCommand As System.Data.Ole Db.OleDbDataAda pter
Dim Tn As TreeNode
Dim CH As TreeNode
Dim DA As OleDb.OleDbData Adapter
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.Ole Db.OleDbConnect ion( _
"provider=Micro soft.Jet.OLEDB. 4.0; " & _
"data source=C:\Inetp ub\wwwroot\inte lis.MDB")
MyCommand = New System.Data.Ole Db.OleDbDataAda pter( _
"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("ENTI TY").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.Cl ose()
MyConnection.Di spose()
End If
End Sub
--
Hutty