Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

VB.NET Treeview control - Databinding to datatable

$
0
0

Hello All. I am using VS2012 & Sql Server 2012 on Win7 x64.

I have a sql table with 'TVNID'(autonumber/identity) 'TVNparID'(shows the identity number where it should be placed in tree) and 'TVNName' (text name to be shown)

I created a data source to the table, and then dragged to the form to create my bindingsource, dataadapter and datatable.

On the form I have a treeview control named 'treeview1' When I open the form all I can see is the Root text. I am having issues with the child nodes being populated. Please look at my code to help me understand my mistakes.

Imports System.Data.SqlClient

Public Class frmTree1

    Private Property TreeNode As TreeNode


    Private Sub frmTree1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'TvwNodesDS.tblTVNodes' table. You can move, or remove it, as needed.
        Me.TblTVNodesTableAdapter.Fill(Me.TvwNodesDS.tblTVNodes)


        For Each dr As DataRow In Me.TvwNodesDS.tblTVNodes
            TreeNode = TreeView1.Nodes.Add(dr("TVNName").ToString())
            PopulateTreeView(Convert.ToInt32(dr("TVNName").ToString()), TreeNode)
        Next
        TreeView1.ExpandAll()

    End Sub

    Private Sub TblTVNodesBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs)
        Me.Validate()
        Me.TblTVNodesBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.TvwNodesDS)

    End Sub

    Private Sub PopulateTreeView(parentId As Integer, parentNode As TreeNode)

        Dim childNode As TreeNode
        For Each dr As DataRow In Me.TvwNodesDS.tblTVNodes
            If parentNode Is Nothing Then
                childNode = TreeView1.Nodes.Add(dr("TVNparID").ToString())
            Else
                childNode = parentNode.Nodes.Add(dr("TVNparID").ToString())
            End If
            PopulateTreeView(Convert.ToInt32(dr("TVNName").ToString()), childNode)
        Next
    End Sub
End Class
Thanks to all in advance with your help


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>