treeview database

G

Guest

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
 
G

Guest

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.TreeViewClickEventArgs) 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
 
G

Guest

Thanks for the reply Sandy. Actually, that's the same code that I used to
get where I am now. I just need to study the child relations a little
deeper. Thanks.

Sandy said:
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.TreeViewClickEventArgs) 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 said:
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
 
P

Patrick Olurotimi Ige

Yeah that code is for populating the treeview from the DB.
But has any of u came across a way to manage it.
I mean a front end where someone can modify ,delete and add nodes
dynamically.
Thanks
 
G

Guest

I had to create a duplicate table to get the child underneath the parent.
Would have preferred using the same table for both. I have a problem with
the display. Instead of displaying the PARENT once and all the CHILD
underneath, the PARENT is being displayed on multiple lines for each CHILD.
I'm trying to figure how to get the code to recognize PARENT already exist
and not to add it again. In addition, when adding CHILD to see if PARENT
exist and add CHILD underneath. Any ideas? Here's my code:

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 MyCommand2 As System.Data.OleDb.OleDbDataAdapter

Dim dt As DataTable
Dim Tn As TreeNode
Dim Tn2 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, ID from Ent1",
MyConnection)
MyCommand2 = New System.Data.OleDb.OleDbDataAdapter( _
"select * from Ent2",
MyConnection)

ds = New DataSet
' dt = ds.Tables(0)

'Add an ENTITY table to the DataSet.
MyCommand.Fill(ds, "ENT1")
MyCommand2.Fill(ds, "ENT2")
ds.Relations.Add("ENTITIES", ds.Tables("ENT1").Columns("ID"),
ds.Tables("ENT2").Columns("ID"))

'Populate the TreeView from the DataSet.
For Each DR In ds.Tables("ENT1").Rows
Tn = New TreeNode
Tn.Text = DR("PARENT")
Tn.NodeData = DR("PARENT")
Tn.Expandable = ExpandableValue.CheckOnce
TreeView1.Nodes.Add(Tn)
For Each DR2 In DR.GetChildRows("ENTITIES")
Tn2 = New TreeNode
Tn2.Text = DR2("CHILD")
Tn.Nodes.Add(Tn2)
Next

Next

'Clean up.
ds.Dispose()
' DA.Dispose()
MyConnection.Close()
MyConnection.Dispose()
End If

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top