make a treeview from database

A

Armin Zingler

QT said:
Dear Sirs,

I have a database table which has 4 column such as

Document Name
Subpart
Title

I want to make a treeview like this

+ Document Name
+Subpart
-Tittle

I try several way but I can not add child nodes after adding document
names. Where can I find good explanation?

Best Regrads

Write a recursive procdedure. Pass a System.Windows.Forms.TreeNodeCollection
collection to the sub. Calling the sub the first time, pass treeview1.nodes.
Within the procedure, add a node and call the procedure passing the node's
Nodes collection.

http://msdn.microsoft.com/library/en-us/vbcn7/html/vaconCreatingRecursiveProcedures.asp
 
K

Ken Tucker [MVP]

Hi,

Add a new node for each record. Here is an example.

Imports System.Data.SqlClient

Public Class Form1

Inherits System.Windows.Forms.Form

Dim ds As New DataSet

Dim dvCustomers As DataView

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim conn As SqlConnection

Dim daCustomers As SqlDataAdapter

Dim daOrders As SqlDataAdapter

Dim strConn As String

strConn = "Data Source = " + SystemInformation.ComputerName

strConn += "\VSdotNet; Initial Catalog = NorthWind;"

strConn += "Integrated Security = SSPI;"

conn = New SqlConnection(strConn)

daCustomers = New SqlDataAdapter("Select * from Employees order by LastName,
FirstName", conn)

daCustomers.Fill(ds, "Clients")

dvCustomers = New DataView(ds.Tables("Clients"))

AddNodes()

End Sub

Private Sub AddNodes()

Dim drCustomer As DataRowView

Dim root As System.Windows.Forms.TreeNode

Dim strName As String

With trvNorthWind

..BeginUpdate()

..Nodes.Clear()

For Each drCustomer In dvCustomers

strName = drCustomer.Item("FirstName") & " " & drCustomer.Item("LastName")

Dim n As TreeNode

n = New TreeNode(strName)

Dim snCountry As TreeNode

Dim snCity As TreeNode

snCity = New TreeNode(drCustomer.Item("City"))

snCountry = New TreeNode(drCustomer.Item("Country"))

snCountry.Nodes.Add(snCity)

n.Nodes.Add(snCountry)

trvNorthWind.Nodes.Add(n)

Next drCustomer

..EndUpdate()

End With

End Sub



End Class

Ken
 
H

Herfried K. Wagner [MVP]

* "QT said:
I have a database table which has 4 column such as

Document Name
Subpart
Title

I want to make a treeview like this

+ Document Name
+Subpart
-Tittle

I try several way but I can not add child nodes after adding document names.
Where can I find good explanation?

Loop though the rows and add the items:

\\\
Dim tnDocument As TreeNode = Me.TreeView1.Nodes.Add(...)
Dim tnSubPart As TreeNode = tnDocument.Nodes.Add(...)
tnSubPart.Nodes.Add(...)
///
 
O

One Handed Man [ OHM# ]

Hey Armin,
You really do know your stuff dont you. I wonder how you
are not an MVP already ?

Regards - OHM

Armin said:
Write a recursive procdedure. Pass a
System.Windows.Forms.TreeNodeCollection collection to the sub.
Calling the sub the first time, pass treeview1.nodes. Within the
procedure, add a node and call the procedure passing the node's Nodes
collection.
http://msdn.microsoft.com/library/en-us/vbcn7/html/vaconCreatingRecursiveProcedures.asp

Regards - OHM# OneHandedMan{at}BTInternet{dot}com
 
Q

QT

Dear Sirs,

I have a database table which has 4 column such as

Document Name
Subpart
Title

I want to make a treeview like this

+ Document Name
+Subpart
-Tittle

I try several way but I can not add child nodes after adding document names.
Where can I find good explanation?

Best Regrads
 
A

Armin Zingler

One Handed Man said:
Hey Armin,
You really do know your stuff dont you. I wonder how
you are not an MVP already ?

I tried to write you an email. {at} replaced by "@" and {dot} replaced by
".". It came back. Still not the right address?
 
O

One Handed Man [ OHM# ]

Sorry, I had to delete this because of huge spam mail

Try

O_H_M(at)BTINTERNET.COM



Armin said:
I tried to write you an email. {at} replaced by "@" and {dot}
replaced by ".". It came back. Still not the right address?

Regards - OHM# OneHandedMan{at}BTInternet{dot}com
 
Q

QT

Dear Armin,

I try to understand your idea and I read link. But I could not compose some
scripts with your idea. May be I have some problem when adding node into
treeview.

I am using following scripts to add node

Dim Node As TreeNode

Node = TreeView1.Nodes.Add("level1")

Node.Nodes.Add("level2")

As far as I know, I should dim level 1 Node and to add childs into it. But I
can not dim document names which are coming from database. So within
recursive procedure, I can not use above script.

Am I using wrong method to add nodes?
 

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

Top