Loading Data into Treeview Control?

T

Troy

I've almost got this the way I want it. I'm loading my customer names
into a treeview control. My problem is I'm repeating my root nodes. I
know it's something to do with my loop structure but My eyes are
crossing from it can someone help me get this organised lol. here's
my code.

Private Sub updateTree()
Dim Conn As Data.OleDb.OleDbConnection = New
Data.OleDb.OleDbConnection
("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
OpenFileDialog1.FileName & ";Persist Security
Info=False;")
Dim DR As Data.OleDb.OleDbDataReader
Dim indx As Short
Dim NoUsers As Boolean
Dim sqlNames As String
Dim currentAlpha As String
Dim sContactName As String

sqlNames = "SELECT ContactID, LastName1, FirstName1,
MiddleInitial1 "
sqlNames = sqlNames & "FROM Contact ORDER BY"
sqlNames = sqlNames & " LastName1, FirstName1,
MiddleInitial1 "
Dim Cmd As Data.OleDb.OleDbCommand = New
Data.OleDb.OleDbCommand
(sqlNames, Conn)

' Clear Treeview Nodes
TreeView1.Nodes.Clear()

Try
Conn.Open()
DR = Cmd.ExecuteReader

If DR.HasRows = False Then
TreeView1.Nodes.Add("No Users on file")
TreeView1.ForeColor = Color.Red
NoUsers = True 'Boolean to tell other objects that
there are no users.
Else
TreeView1.ForeColor = Color.Black
NoUsers = False 'Boolean to tell other objects that
there are users. End If

While DR.Read

For indx = Asc("A") To Asc("Z")
currentAlpha = Chr(indx)
TreeView1.Nodes.Add(New
TreeNode(currentAlpha))

' Add a child TreeNode for each Customer
object in the current Alpha
Character.
If
UCase(Microsoft.VisualBasic.Left(DR("LastName1"), 1)) = currentAlpha

Then
sContactName = DR("Lastname1") & ", "
& DR("FirstName1") & " " &
DR("MiddleInitial1") & "."

System.Windows.Forms.Application.DoEvents()
TreeView1.Nodes(indx - 65).Nodes.Add(New
TreeNode(sContactName))

End If

Next
End While
DR.Close()
Conn.Close()
End If
Catch LX As Exception
MsgBox(LX.Message, MsgBoxStyle.Exclamation, "")
End Try
TreeView1.ExpandAll()
 
L

Larry Lard

This is what your code is doing at the moment:

- For each row in the data set
- - create 26 root nodes, one for each letter
- - put this row under the appropriate letter node

Which is why you are getting (I assume) repeating root nodes. What I
recommend you do is this:

- have 'currentLetter' and 'currentNode' variables, initially set to ""
and Nothing
- for each row in the data set
- - if this is a new letter (that is, if left(name,1) <> currentletter)
then:
- - - set currentLetter to left(name, 1)
- - - create a new root node with this letter as its text
- - end if
- - put this row in a new node under currentNode
- next

Note that this method will note create 'empty' root letter notes, ie if
there are no names beginning with X, there will be no X node, etc

How's that work for you?
 
K

Ken Tucker [MVP]

Hi,

Maybe this will help.

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()

trvNorthWind.Sorted = True

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



Ken
-------------------------
I've almost got this the way I want it. I'm loading my customer names
into a treeview control. My problem is I'm repeating my root nodes. I
know it's something to do with my loop structure but My eyes are
crossing from it can someone help me get this organised lol. here's
my code.

Private Sub updateTree()
Dim Conn As Data.OleDb.OleDbConnection = New
Data.OleDb.OleDbConnection
("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
OpenFileDialog1.FileName & ";Persist Security
Info=False;")
Dim DR As Data.OleDb.OleDbDataReader
Dim indx As Short
Dim NoUsers As Boolean
Dim sqlNames As String
Dim currentAlpha As String
Dim sContactName As String

sqlNames = "SELECT ContactID, LastName1, FirstName1,
MiddleInitial1 "
sqlNames = sqlNames & "FROM Contact ORDER BY"
sqlNames = sqlNames & " LastName1, FirstName1,
MiddleInitial1 "
Dim Cmd As Data.OleDb.OleDbCommand = New
Data.OleDb.OleDbCommand
(sqlNames, Conn)

' Clear Treeview Nodes
TreeView1.Nodes.Clear()

Try
Conn.Open()
DR = Cmd.ExecuteReader

If DR.HasRows = False Then
TreeView1.Nodes.Add("No Users on file")
TreeView1.ForeColor = Color.Red
NoUsers = True 'Boolean to tell other objects that
there are no users.
Else
TreeView1.ForeColor = Color.Black
NoUsers = False 'Boolean to tell other objects that
there are users. End If

While DR.Read

For indx = Asc("A") To Asc("Z")
currentAlpha = Chr(indx)
TreeView1.Nodes.Add(New
TreeNode(currentAlpha))

' Add a child TreeNode for each Customer
object in the current Alpha
Character.
If
UCase(Microsoft.VisualBasic.Left(DR("LastName1"), 1)) = currentAlpha

Then
sContactName = DR("Lastname1") & ", "
& DR("FirstName1") & " " &
DR("MiddleInitial1") & "."

System.Windows.Forms.Application.DoEvents()
TreeView1.Nodes(indx - 65).Nodes.Add(New
TreeNode(sContactName))

End If

Next
End While
DR.Close()
Conn.Close()
End If
Catch LX As Exception
MsgBox(LX.Message, MsgBoxStyle.Exclamation, "")
End Try
TreeView1.ExpandAll()
 
T

Troy

LOL thanks guys I got it to work but here's the kicker.

Works perfect but using Visual Basic .NET 2005 Beta and according to
the tooltip for my fix the command is being taken out after beta is
over and no longer used so back to square one on this.

Here's the code that works though:
Private Sub updateTree()
Dim Conn As Data.OleDb.OleDbConnection = New
Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=" & _
OpenFileDialog1.FileName & ";Persist Security
Info=False;")
Dim DR As Data.OleDb.OleDbDataReader
Dim indx As Short
Dim NoUsers As Boolean
Dim sqlNames As String
Dim currentAlpha As String
Dim sContactName As String
sqlNames = "SELECT ContactID, LastName1, FirstName1,
MiddleInitial1 "
sqlNames = sqlNames & "FROM Contact ORDER BY"
sqlNames = sqlNames & " LastName1, FirstName1,
MiddleInitial1 "
Dim Cmd As Data.OleDb.OleDbCommand = New
Data.OleDb.OleDbCommand(sqlNames, Conn)

' Clear Treeview Nodes
TreeView1.Nodes.Clear()


Try

Conn.Open()
DR = Cmd.ExecuteReader
If DR.HasRows = False Then
TreeView1.Nodes.Add("No Users on file")
TreeView1.ForeColor = Color.Red
NoUsers = True 'Boolean to tell other objects that
there are no users.
Else
TreeView1.ForeColor = Color.Black
NoUsers = False 'Boolean to tell other objects that
there are users.

For indx = Asc("A") To Asc("Z")
currentAlpha = Chr(indx)
TreeView1.Nodes.Add(New TreeNode(currentAlpha))


While DR.Read()
' Add a child TreeNode for each Customer
object in the current Alpha Character.

If
UCase(Microsoft.VisualBasic.Left(DR("LastName1"), 1)) = currentAlpha
Then

sContactName = DR("Lastname1") & ", "
& DR("FirstName1") & " " & DR("MiddleInitial1") &
"."


System.Windows.Forms.Application.DoEvents()

TreeView1.Nodes(indx - 65).Nodes.Add(New
TreeNode(sContactName, 1, 1))


End If
End While
DR.Restart()
Next
DR.Close()
Conn.Close()
End If





Catch LX As Exception
MsgBox(LX.Message, MsgBoxStyle.Exclamation, "")
End Try
TreeView1.ExpandAll()

The command in [b:1553f9a06b]BOLD[/b:1553f9a06b] is what is being
taken out after Beta is over.

Anyone else know of a way to reset the stack pointer to the top of the
DB again once it had gone through the database once?

or

Offer another approach using the OleDBConnection method?
 

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