Loading Data into Treeview Control?

Troy

Well-known member
Joined
Feb 7, 2005
Messages
153
Programming Experience
10+
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()
 

Troy

Well-known member
Joined
Feb 7, 2005
Messages
153
Programming Experience
10+
Thread moved?

I don't know why my thread got moved I am using VB.NET 2005 Beta.

Although I have fixed my problem, in a way that itself caused a problem.

Everything works as I want with the exception that the command I'm using says not to use it as it is obsolete and will be removed after Beta.

So now my question is can you help me use the same OleDatabase structure but a different method to populate my treeview.

The now working code is as follows:

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

ToolStripProgressBar1.Value = Math.Abs(4 * (indx - 65))

StatusStrip1.Items.Item(0).Text = "Populating Treeview with database information."

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

ToolStripProgressBar1.Value = 0

StatusStrip1.Items.Item(0).Text = "Populating Treeview with database done."





Catch LX As Exception

MsgBox(LX.Message, MsgBoxStyle.Exclamation, "")

End Try

TreeView1.ExpandAll()

Me.ContactTableAdapter.Fill(Me.MHManagerDataSet.Contact)

System.Windows.Forms.Application.DoEvents()

End Sub

As I said it is working for the time being but the .Restart command will/is now obsolete according to Microsoft.

Anyone know another method to restart the .read process or move the stack pointer back to the top of the database to run again for the next alpha character in my loop?
 
Top Bottom