Skip to content

( RESOLVED ) VBNET2008 using DataReader to Fill TreeView control at Runtime

Lennie Kuah

Asked Feb 10, 2011, 4:45 PM EST1 answer
Share
LinkedInX
Hullo Good Friends,

I am using

Window Application using VBNET2008, DataReader and TreeView  

I need your help, Please help me.

 

I am trying to use DATAREADER to fill the TreeView control during Runtime

I am using TreeView controls and having problem doing the coding as I have not done it before.

 

Listed beow are the SQL String and the result of it. And also the graphic drawing of TreeView  display  

 

SQL String :

SELECT  OrderID,  OrderDate  ProductID

FROM   Invoices WHERE (CustomerID = 'Chops')  Order by OrderID, Orderdate

 

Part of the 137 rows Result of SQL String

OrderId         OrderDate     ProdID

10254          11/07/1996        74

10254          11/07/1996        24

10254          11/07/1996        55

10254          13/07/1996       203

10254          13/07/1996       112

10370          03/12/1996        74

10370          03/12/1996         1

10370          03/12/1996        64

10370          03/12/1996        56

10519          28/04/1997        10

10519          28/04/1997        60

 

 

TreeView Display Format

Graphic drawing of how it should look like

 

10254  '=è Parent

      11/07/1996 ' =èChild

                74 '=======è GrandChild

                24

                55

      13/07/1996   '=èChild

               203 '====è GrandChild

               112

 

10370 '=è Parent

           03/12/1996   '=èChild

                74  '====è GrandChild

                1

                64

                56
Thank you very much for helping me. Once with your help I got my coding working I will post it here to share with other Newbies who may have similar problem and also an appreciation of your help to share with others.
 
The overall coding are attached as ZIP File
 
 
Cheers,
TeeLeong
 

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Hullo Friends,
    Finally I have resolved the problem of VBNET2008 with TreeView in my earlier posting here asking for help.
    What I did was I was using debugging event on every coding line to evaliate it's event and finally identified the problem cause my TreeView Nodes index which I use differenet variables from Parent Nodes, Child Nodes and Grandchild Nodes.
    Here are the working coding and I wish to share with other newbies who may be having similar problem and without receiving and helps at all. 

    Private Sub FFillTreeViewNode()

     

            Dim intOrderID As Integer = 0

            Dim dteOrderDate As String = String.Empty

            Dim intCNode As Integer = 0

            Dim intGCNode As Integer = 0   

            Dim bolJustNewParent As Boolean = True

     

            Dim strsql As String

          strsql = "Select OrderID, Convert(varchar(10), OrderDate, 103) as [OrderDate], ProductID "

            strsql += " From TblSalesInvoices "

            strsql += " Where (CustomerID = 'FRAB' )"

            strsql += " Order By OrderID, OrderDate "

     

            sqlconn = New SqlConnection(connstr)

            sqlcmd = New SqlCommand(strsql, sqlconn)

            sqlcmd.Connection.Open()

            DR = sqlcmd.ExecuteReader

     

            While (DR.Read())

     

                If (DR.Item("OrderID") <> intOrderID) Then     '

     

                    If bolJustNewParent = True Then

                        bolJustNewParent = False              

                    Else

                        intCNode += 1

                        intGCNode = 0

                    End If

     

                    '--- Parent ---

                    Me.TreeViewTest.Nodes.Add(DR.Item("OrderID").ToString)

                    intOrderID = DR.Item("OrderID")

     

                    '--- Child ----

                    Me.TreeViewTest.Nodes(intCNode).Nodes.Add(DR.Item("OrderDate").ToString)

                    dteOrderDate = DR.Item("OrderDate").ToString

                End If

     

                ' --- Child ---

                If (DR.Item("OrderDate").ToString <> dteOrderDate) Then

     

                    Me.TreeViewTest.Nodes(intCNode).Nodes.Add(DR.Item("OrderDate").ToString)

                    dteOrderDate = DR.Item("OrderDate").ToString

                    intGCNode += 1

     

                    If intOrderID <> DR.Item("OrderID") Then

                        intCNode += 1

                    End If

     

                End If

                '---grandchild

                Me.TreeViewTest.Nodes(intCNode).Nodes(intGCNode).Nodes.Add(DR.Item("ProductID"))

            End While

     

            sqlconn.Close()

            DR.Close()

            sqlcmd.Dispose()

        End Sub

     

     

      Private Sub TreeViewTest_AfterSelect(ByVal sender As System.Object,

                ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeViewTest.AfterSelect

            Me.txtTVItem.Text = Me.TreeViewTest.SelectedNode.FullPath.ToString

       End Sub
    Hi, Newbies friends, Hope my working coding will help you.
     

    0