Load data as treeview roots and nodes

GregW

New member
Joined
Jan 24, 2005
Messages
2
Location
Cape Town
Programming Experience
1-3
Can someone please help me? I'm trying to create a treeview control which uses records in my database as it's roots and nodes. I cannot find a reference to this anywhere. Can someone steer me in the right direction.

much obliged.
Greg
 
GregW said:
Can someone please help me? I'm trying to create a treeview control which uses records in my database as it's roots and nodes. I cannot find a reference to this anywhere. Can someone steer me in the right direction.

much obliged.
Greg


Here's some Code I wrote to populate my Treeview with my Database information using OLEDB hope if may help demystify some things.

PublicSub PopulateTreeview()

Dim strConnection AsString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " & OpenFileDialog1.FileName

Dim cn As System.Data.OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection(strConnection)

Dim da As System.Data.OleDb.OleDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter("SELECT * FROM Contact", cn)

Dim ds AsNew System.Data.DataSet()

Dim indx AsShort

Dim currentAlpha AsString

Dim sContactName AsString

OnErrorGoTo Handler

' Fill the DataTable

da.Fill(ds, "Contact")

Dim dt As System.Data.DataTable = ds.Tables.Item("Contact")

Dim rowCustomer As System.Data.DataRow

rowCustomer =
Nothing

'Populate Treeview Control

For indx = Asc("A") To Asc("Z")

currentAlpha = Chr(indx)

TreeView1.Nodes.Add(
New TreeNode(currentAlpha))

' Add a child TreeNode for each Author in the matching Main Node.

ForEach rowCustomer In dt.Rows

If (UCase(Mid(rowCustomer.Item("LastName1"), 1, 1)) = currentAlpha) Then

sContactName = rowCustomer.Item("LastName1") & ", " & rowCustomer.Item("FirstName1") & " " & rowCustomer.Item("MiddleInitial1") & "."

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

EndIf

Next

Next

TreeView1.Enabled = True

TreeView1.ExpandAll()

ExitSub

Handler:

'Display an error to the user.

MsgBox("An error occurred. Error Number: " & Err.Number & _

" Description: " & Err.Description & " Source: " & Err.Source, MsgBoxStyle.Critical, "ERROR!")

EndSub






Troy
 
Back
Top