Pergunta

I have two different tables from a database i am using for populating a treeview in vb 2008, i have already successfuly displayed the parent nodes to the treeview. my problem is displaying the child nodes to each node. i have tried treeview1.nodes("node text here").nodes.add("node text here") but the problem is for me to find the node i am using an oledbdatareader. here is my current code...

Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient

Public Class Form1
    Dim mypath = "I:\Casegoods\Database\Expertpetrolia.mdb"
    Dim conn As New OleDbConnection("Provider = microsoft.ace.oledb.12.0;Data Source=" & mypath)
    Dim cmd As OleDbCommand
    Dim dr As OleDbDataReader
    Dim sql1 = "SELECT Due_Date, Date_id FROM Supply_Date ORDER BY [Due_Date]"

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


    Dim cmd As OleDbCommand
    Using conn

        cmd = New OleDbCommand(sql1, conn)
        conn.Open()

        dr = cmd.ExecuteReader

        If dr.HasRows Then
            Do While dr.Read()
                Dim parent As String = (dr.Item("Due_Date"))

                If DateDiff(Microsoft.VisualBasic.DateInterval.Day, Today, (dr.Item("Due_Date"))) > -10 Then
                    TreeView1.Nodes.Add(parent)
                End If
            Loop
        Else
            MsgBox("No Things found")
        End If

        dr.Close()

        End Using

    End Sub
End Class

im not sure if i need another oledbdatareader for reading the other table or another oledbcommand or what. i am having major coding block and i cant seem to come to a solution.

Foi útil?

Solução

If there are many records (>1) in Total_Purchase related to each Supply row, you will appear to get "repeating data" - the data from Supply will repeat because it is related to Total_Purchase and is part of the row/record as per your SQL INNER JOIN.

I strongly advise looking at the query in Access to understand what the dataset looks like.

The issue becomes adding child nodes in a loop until the Due_Date changes:

Dim parent As String = ""
Dim parentNode As TreeNode
If dr.HasRows Then
    Do While dr.Read()
        If (parent = (dr.Item("Due_Date"))) Then     ' same parent, add child
            ' add child node (presumably from tbl B data)
        Else
             parent = (dr.Item("Due_Date"))    ' set NEW parent value
             ' save parent node for adding child nodes above
             parentNode = TreeView1.Nodes.Add(parent)
        End If
    Loop
Else

Assuming the SQL is correct, you only add a new parent when it changes, otherwise add a child nodes for this parent until you detect a new Due_Date/parent.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top