Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27349

VS 2010 Need Help w/ TreeViews

$
0
0
First time poster here so I appreciate any help that can be offered.

I've spent 3 days researching the answer to this question to no avail; So I apologize in advance if this has already been posted (I was not able to find it).

Situation:

I'm running a SQL query and dumping the results *currently* into a single TreeView; what'd I'd like to do is Dump the ParentNode and any associated ChildNodes into a TreeView and then move on to dump the next set of ParentNode/ChildNodes into a second treeview and so on so forth. I would think this would be simple to do, but no, at least not for me.

The TreeView controls are already created on the form and named (tv1 through tv18)

The functional Code to dump all data into a single TreeView is as follows:
Code:

Dim DS As DataSet
        Dim CN1 As New SqlClient.SqlConnection(CONA.ConnectionString)
        Dim DAResources As New SqlClient.SqlDataAdapter("Select p.resourceID ResourceID, COALESCE(r.RESOURCE,p.Resource) Resource, ISNULL(p.PROJECTS,0) [PROJECTS] from (SELECT resource_id as ResourceID, full_name as RESOURCE FROM wh_resource WHERE is_active = 1) r FULL JOIN (SELECT COUNT(DISTINCT PROJECT_NUMBER) AS PROJECTS, WH_RESOURCE.FULL_NAME AS RESOURCE, project_lead_resource_id AS ResourceID FROM WH_PROJECT LEFT JOIN wh_RESOURCE ON wh_PROJECT.project_lead_resource_id = wh_RESOURCE.RESOURCE_ID WHERE PROJECT_STATUS_ID != 5 GROUP BY ALL WH_RESOURCE.FULL_NAME, wh_PROJECT.project_lead_resource_id) p on r.resourceID=p.resourceID WHERE COALESCE(r.[ResourceID],p.resourceID) IN (" & SelectedPResources & ") ORDER BY r.[Resource],p.resource", CN1)
        Dim DAPDetails As New SqlClient.SqlDataAdapter("Select p.resourceID ResourceID, COALESCE(r.RESOURCE,p.Resource) Resource, ISNULL(p.PROJECTS,0) Projects, p.Account 'Account', p.[Project Title] 'Project Title', p.STATUS Status, p.Completed 'Completed %', p.[Status Detail] 'Status Detail' from (SELECT resource_id as ResourceID, full_name as RESOURCE FROM wh_resource WHERE is_active = 1) r FULL JOIN (SELECT project_lead_resource_id AS ResourceID, WH_RESOURCE.FULL_NAME AS RESOURCE, PROJECT_NUMBER AS PROJECTS, wh_account.account_name as 'Account', project_name as 'Project Title', wh_project_status.project_status_name AS STATUS, completed_percentage AS 'Completed', status_detail AS 'Status Detail' FROM WH_PROJECT LEFT JOIN wh_RESOURCE ON wh_PROJECT.project_lead_resource_id = wh_RESOURCE.RESOURCE_ID LEFT JOIN wh_account ON wh_project.account_id = WH_ACCOUNT.account_id LEFT JOIN wh_project_status ON WH_PROJECT.project_status_id = WH_PROJECT_STATUS.project_status_id WHERE WH_PROJECT.PROJECT_STATUS_ID != 5) p on r.resourceID=p.resourceID WHERE COALESCE(r.[ResourceID],p.resourceID) IN (" & SelectedPResources & ") ORDER BY r.[Resource],p.resource", CN1)

        DS = New DataSet()
        CN1.Open()
        DAResources.Fill(DS, "dtResources")
        DAPDetails.Fill(DS, "dtDetails")
CN1.Close()

DS.Relations.Add("ProjectDetails", DS.Tables("dtResources").Columns("resourceID"), DS.Tables("dtDetails").Columns("resourceID"))

tv1.Nodes.Clear()

Dim i, n As Integer
        Dim parentrow As DataRow
        Dim ParentTable As DataTable
        ParentTable = DS.Tables("dtResources")

        For Each parentrow In ParentTable.Rows
            Dim parentnode As TreeNode
            parentnode = New TreeNode(parentrow.Item(1) & " (" & parentrow.Item(2) & ")")
            TreeView1.Nodes.Add(parentnode)

            Dim childrow As DataRow
            Dim childnode As TreeNode
            Dim childnode2 As TreeNode
            childnode = New TreeNode()
            childnode2 = New TreeNode

            For Each childrow In parentrow.GetChildRows("ProjectDetails")
                childnode = parentnode.Nodes.Add("[" & childrow(2) & "] - " & childrow(3) & " - " & childrow(6) & "%")
                childnode.ToolTipText = ("[" & childrow(2) & "] " & vbCrLf & "Account: " & childrow(3) & " | Status: " & childrow(5) & " | Completed: " & childrow(6) & "% " & vbCrLf & "Title: " & childrow(4) & vbCrLf & "Detail: " & childrow(7))
                childnode2 = childnode.Nodes.Add("[" & childrow(2) & "] " & vbCrLf & "Account: " & childrow(3) & " | Status: " & childrow(5) & " | Completed: " & childrow(6) & "% " & vbCrLf & "Title: " & childrow(4) & vbCrLf & "Detail: " & childrow(7))
                childnode.Tag = childrow("resourceID")
            Next childrow
        Next parentrow

As already mentioned the above code works flawlessly in displaying all Nodes within a single TreeView (tv1). The code below is my attempt to dump only a ParentNode and associated ChildNodes into tv1, then move on to dump the next ParentNode and associated ChildNodes into tv2, so on so forth...

Code:


Dim i, n, tvNum As Integer
        Dim parentrow As DataRow
        Dim ParentTable As DataTable
        ParentTable = DS.Tables("dtResources")

        For Each parentrow In ParentTable.Rows
            tvNum += 1
            Dim NewTreeView As New TreeView
            Dim TreeViewName As String = "tv" & tvNum
            NewTreeView.Name = TreeViewName

            Dim parentnode As TreeNode
            parentnode = New TreeNode(parentrow.Item(1) & " (" & parentrow.Item(2) & ")")

            NewTreeView.Nodes.Add(parentnode)

            Dim childrow As DataRow
            Dim childnode As TreeNode
            Dim childnode2 As TreeNode
            childnode = New TreeNode()
            childnode2 = New TreeNode

            For Each childrow In parentrow.GetChildRows("ProjectDetails")
                childnode = parentnode.Nodes.Add("[" & childrow(2) & "] - " & childrow(3) & " - " & childrow(6) & "%")
                childnode.ToolTipText = ("[" & childrow(2) & "] " & vbCrLf & "Account: " & childrow(3) & " | Status: " & childrow(5) & " | Completed: " & childrow(6) & "% " & vbCrLf & "Title: " & childrow(4) & vbCrLf & "Detail: " & childrow(7))
                childnode2 = childnode.Nodes.Add("[" & childrow(2) & "] " & vbCrLf & "Account: " & childrow(3) & " | Status: " & childrow(5) & " | Completed: " & childrow(6) & "% " & vbCrLf & "Title: " & childrow(4) & vbCrLf & "Detail: " & childrow(7))
                childnode.Tag = childrow("resourceID")
            Next childrow
        Next parentrow

When I execute the above code, I receive no errors or exceptions; but the Treeview controls on the form never populate with data. I am sure I am missing something trivial, but I'm knocking my head against a wall trying to get this sorted out and cannot.

Again, any help is appreciated!

Viewing all articles
Browse latest Browse all 27349

Trending Articles