potnoodle13
asked on
Populate a treeview
Hi experts,
I've been tasked with populating a treeview within vb.net (windows application)
I've been given the SQL and can successfully connect to the DB and return the data into a datatable.
the data looks like this....
USER_ID,NAME,ROLE_ID,ROLE_ DESC,ACTIV E_IND
1,ANDREW SIMPSON,1,Information,Y
1,ANDREW SIMPSON,2,Registry,Y
2,GEORGE Binns,1,Information,Y
2,GEORGE Binns,3,Charge,Y
5,PAUL BOWMAN,1,Information,Y
5,PAUL BOWMAN,2,Registry,Y
5,PAUL BOWMAN,3,Charge,Y
88,MICK JAGGER,1,Information,Y
88,MICK JAGGER,2,Registry,Y
88,MICK JAGGER,3,Charge,Y
However i'm struggling to populate a treeview it should look like
Name
Role description
Role description
Name
Role description
So should look like
Andrew Simpson
Information
Registry
George Binns
Information
Charge
etc etc.....
Any help would be very useful as i've not used the treeview before and struggling at the moment
Thanks
Mark
I've been tasked with populating a treeview within vb.net (windows application)
I've been given the SQL and can successfully connect to the DB and return the data into a datatable.
the data looks like this....
USER_ID,NAME,ROLE_ID,ROLE_
1,ANDREW SIMPSON,1,Information,Y
1,ANDREW SIMPSON,2,Registry,Y
2,GEORGE Binns,1,Information,Y
2,GEORGE Binns,3,Charge,Y
5,PAUL BOWMAN,1,Information,Y
5,PAUL BOWMAN,2,Registry,Y
5,PAUL BOWMAN,3,Charge,Y
88,MICK JAGGER,1,Information,Y
88,MICK JAGGER,2,Registry,Y
88,MICK JAGGER,3,Charge,Y
However i'm struggling to populate a treeview it should look like
Name
Role description
Role description
Name
Role description
So should look like
Andrew Simpson
Information
Registry
George Binns
Information
Charge
etc etc.....
Any help would be very useful as i've not used the treeview before and struggling at the moment
Thanks
Mark
ASKER
thank you for the quick response however as you said that is C# not VB.NET i've checked the link and it doesn't help as it doesn't have any database connection at all
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Once you have the datatable, you can get the unique names into another table
assuming that dTable is the datatable
Dim dNames As DataTable = dTable.DefaultView.ToDataT able(true, "Name")
For i as integer = 0 to dNames.Rows.Count - 1
MyTree.Nodes.Add(New TreeNode(dNames.Rows(i).It em("Name") ))
Next
Then handle the BeforeExpand event of tree.
dTable.DefaultView.RowFilt er = "Name='" & e.Node.Text & "'"
For i As Integer = 0 to dTable.DefaultView.Count - 1
e.Node.Nodes.Add(New TreeNode(dTable.DefaultVie w.Item(i). Item("Role _Desc")))
Next
assuming that dTable is the datatable
Dim dNames As DataTable = dTable.DefaultView.ToDataT
For i as integer = 0 to dNames.Rows.Count - 1
MyTree.Nodes.Add(New TreeNode(dNames.Rows(i).It
Next
Then handle the BeforeExpand event of tree.
dTable.DefaultView.RowFilt
For i As Integer = 0 to dTable.DefaultView.Count - 1
e.Node.Nodes.Add(New TreeNode(dTable.DefaultVie
Next
http://www.dotnetperls.com/treeview
TreeView is a collection of nodes, so you would populate nodes and subnodes and then add them to the treeview.