OK, this is just a personal project of mine. I like the treeview control but it seems so dam hard to get it work corectly. The following code is a slash about of Microsofts How To on the treeview.
My db has three tables.
tblStudent = studentname, studentID
tblProgramme = studentID, programme, programmeID
tblResult = result, programeID
Everything populates as it should. I get a parent with student name, a child node with programme and child node 2 with result. BUT there are two problems.
1. If a student enters 2 programmes that have the same name but different result, I get 2 child nodes with the same programme name and 2 child node 2 with the two different results.
2. The child nodes also have their respective ID numbers showing as well eg John Smith will be the parent and his child programme node will be - 1 Swimming 1 - as 1 will be his studentID and the other 1 is the programmeID.
I know I need to loop through the programme records but I have no idea how to go about it, so I end up with 1 child node of the programme name and child node 2 of the two results.
I have no idea how to remove the numbers in the child node. If I remove them from the dataset, the whole thing doesnt work.
Any ideas, pointers, tips about how to get round my two issues?
My db has three tables.
tblStudent = studentname, studentID
tblProgramme = studentID, programme, programmeID
tblResult = result, programeID
Code:
Dim ds, ds1, ds2 As New DataSet
Dim SQLString As String = "SELECT studentname, studentID FROM tblStudent"
Dim SQLString1 As String = "SELECT studentID, programme, programmeID FROM tblProgramme where studentID in (select studentID from tblStudent)"
Dim SQLString2 As String = "Select result, programmeID from [tblResult] where programmeID in (SELECT programmeID FROM tblprogramme)"
Dim da As System.Data.OleDb.OleDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter(SQLString, con)
Dim da1 As System.Data.OleDb.OleDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter(SQLString1, con)
Dim da2 As System.Data.OleDb.OleDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter(SQLString2, con)
con.Open()
da.Fill(ds, "dtCustomers")
da1.Fill(ds, "dtOrders")
da2.Fill(ds, "dtOrderDetails")
'Close the connection to the data store; free up the resources
con.Close()
'Create a data relation object to facilitate the relationship between the Customers and Orders data tables.
ds.Relations.Add("CustToOrd", ds.Tables("dtCustomers").Columns("studentID"), ds.Tables("dtOrders").Columns("studentID"), False)
ds.Relations.Add("OrdToDet", ds.Tables("dtOrders").Columns("programmeID"), ds.Tables("dtOrderdetails").Columns("programmeID"), False)
'''''''''''''''''''''''
TreeView1.Nodes.Clear()
Dim i, n As Integer
Dim parentrow As DataRow
Dim ParentTable As DataTable
ParentTable = ds.Tables("dtCustomers")
For Each parentrow In ParentTable.Rows
Dim parentnode As TreeNode
parentnode = New TreeNode(parentrow.Item(0))
TreeView1.Nodes.Add(parentnode)
''''populate child'''''
'''''''''''''''''''''''
Dim childrow As DataRow
Dim childnode As TreeNode
childnode = New TreeNode()
For Each childrow In parentrow.GetChildRows("CustToOrd")
childnode = parentnode.Nodes.Add(childrow(0) & " " & childrow(1) & " " & childrow(2))
childnode.Tag = childrow("programme")
''''populate child2''''
''''''''''''''''''''''''''
Dim childrow2 As DataRow
Dim childnode2 As TreeNode
childnode2 = New TreeNode()
For Each childrow2 In childrow.GetChildRows("OrdToDet")
childnode2 = childnode.Nodes.Add(childrow2(0))
Next childrow2
''''''''''''''''''''''''
Next childrow
'''''''''''''''
Next parentrow
1. If a student enters 2 programmes that have the same name but different result, I get 2 child nodes with the same programme name and 2 child node 2 with the two different results.
2. The child nodes also have their respective ID numbers showing as well eg John Smith will be the parent and his child programme node will be - 1 Swimming 1 - as 1 will be his studentID and the other 1 is the programmeID.
I know I need to loop through the programme records but I have no idea how to go about it, so I end up with 1 child node of the programme name and child node 2 of the two results.
I have no idea how to remove the numbers in the child node. If I remove them from the dataset, the whole thing doesnt work.
Any ideas, pointers, tips about how to get round my two issues?