[.NET 2.0+] Reading/Writing TreeView Node Structures-VBForums
Results 1 to 1 of 1

Thread: [.NET 2.0+] Reading/Writing TreeView Node Structures

  1. #1

    Thread Starter
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    [.NET 2.0+] Reading/Writing TreeView Node Structures

    C# Version Here

    I've seen many people use TreeViews for the purpose of creating and storing a structure. The following is code that will let you load a DataTable into a TreeView and also write a TreeView's nodes to a DataTable. There are 5 methods to this, of those, only 2 are called by you. The other 3 are for the recursion. I have contained all these methods in a class I called TreeViewrw.

    Function #1: ToTable - Creates a DataTable from the nodes in the passed TreeView.

    vb.net Code:
    1. Public Function ToTable(ByVal tv As TreeView) As DataTable
    2.         Dim dt As New DataTable("TreeNodes")
    3.         Dim id As Integer = 1
    4.  
    5.         dt.Columns.Add("ParentID", GetType(Integer))
    6.         dt.Columns.Add("NodeID", GetType(Integer))
    7.         dt.Columns.Add("Name", GetType(String))
    8.  
    9.         For Each pn As TreeNode In tv.Nodes
    10.             dt.Rows.Add(0, id, pn.Name)
    11.             Me.GetAllNodes(dt, pn, id, id)
    12.             id += 1
    13.         Next
    14.  
    15.         Return dt
    16.     End Function
    17.  
    18. Private Sub GetAllNodes(ByRef dt As DataTable, ByVal pn As TreeNode, ByVal parentid As Integer, ByRef id As Integer)
    19.         For Each cn As TreeNode In pn.Nodes
    20.             id += 1
    21.             dt.Rows.Add(parentid, id, cn.Name)
    22.             Me.GetAllNodes(dt, cn, id, id)
    23.         Next
    24.     End Sub

    Examples of use are at the bottom.

    Function #2: FromTable - Reads the passed DataTable and returns an Array of TreeNodes representing the DataTable's contents.

    vb.net Code:
    1. Public Function FromTable(ByVal dt As DataTable, ByVal parent As String, ByVal node As String, ByVal name As String) As TreeNode()
    2.         Dim tnodes As New List(Of TreeNode)()
    3.         Dim columns As String() = New String(2) {parent, node, name}
    4.  
    5.         If dt.Columns.Contains(parent) AndAlso dt.Columns.Contains(node) AndAlso dt.Columns.Contains(name) Then
    6.             dt.DefaultView.RowFilter = "[" & parent & "] = 0"
    7.             For Each r As DataRowView In dt.DefaultView
    8.                 tnodes.Add(Me.ReadNodes(columns, r, dt))
    9.             Next
    10.             Return tnodes.ToArray()
    11.         Else
    12.             Return tnodes.ToArray()
    13.         End If
    14.     End Function
    15.  
    16.     Private Function ReadNodes(ByVal columns As String(), ByVal r As DataRowView, ByVal dt As DataTable) As TreeNode
    17.         Dim cn As New TreeNode()
    18.  
    19.         cn.Name = r(columns(2)).ToString()
    20.         cn.Text = cn.Name
    21.  
    22.         Me.GetAllNodes(columns, cn, dt, CInt(r(columns(1))))
    23.         Return cn
    24.     End Function
    25.  
    26. Private Sub GetAllNodes(ByVal columns As String(), ByRef pn As TreeNode, ByVal dt As DataTable, ByVal nodeid As Integer)
    27.         dt.DefaultView.RowFilter = ("[" & columns(0).ToString() & "] = ") + nodeid.ToString()
    28.  
    29.         For Each r As DataRowView In dt.DefaultView
    30.             Dim cn As New TreeNode()
    31.  
    32.             cn.Name = r(columns(2)).ToString()
    33.             cn.Text = cn.Name
    34.  
    35.             pn.Nodes.Add(cn)
    36.             Me.GetAllNodes(columns, cn, dt, CInt(r(columns(1))))
    37.         Next
    38.     End Sub

    Examples of use are at the bottom.

    Examples:

    ToTable

    For this function there is 1 parameter, simple enough. You pass the TreeView that you want a DataTable representation of it's nodes and it returns it.

    Use:

    vb.net Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim TreeViewRW As New TreeViewRW
    3.         Me.DataGridView1.DataSource = TreeViewRW.ToTable(Me.TreeView1)
    4.     End Sub

    Doesn't get much simpler then that. So assuming you had Button1, DataGridView1 and TreeView1 on the form, and TreeView1's nodes looked like this:



    Clicking that button would yield this:



    FromTable

    For this function there are 4 parameters:

    The first (dt) is the DataTable containing the data to read.

    The second (parent) is the column name in the DataTable that contains what is called the ParentID.

    The third (node) is the column name in the DataTable that contains the unique NodeID.

    The fourth (name) is the column name in the DataTable that contains the value that will be assigned to both the node's name and text properties.

    Use:

    vb.net Code:
    1. Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    2.         Dim tvrw As New TreeViewRW()
    3.         Dim dt As New DataTable("TreeNodes")
    4.         dt.Columns.Add("ParentID", GetType(Integer))
    5.         dt.Columns.Add("NodeID", GetType(Integer))
    6.         dt.Columns.Add("Name", GetType(String))
    7.  
    8.         dt.Rows.Add(0, 1, "Parent 1")
    9.         dt.Rows.Add(0, 2, "Parent 2")
    10.         dt.Rows.Add(1, 3, "Child 1")
    11.         dt.Rows.Add(1, 4, "Child 2")
    12.         dt.Rows.Add(3, 5, "Child 3")
    13.         dt.Rows.Add(2, 6, "Child 4")
    14.  
    15.         Me.TreeView1.Nodes.AddRange(tvrw.FromTable(dt, "ParentID", "NodeID", "Name"))
    16.     End Sub

    So as you can see I created a small sample DataTable to show how the data is read. Just by looking at the rows it's easy to tell that Child 1's parent is Parent 1, Child 3's parent is Child 1, etc... Assuming you had Button1 and TreeView1 on the form, clicking the button would yield this:





    Here is the class:

    vb.net Code:
    1. Public Class TreeViewRW
    2.     Public Function ToTable(ByVal tv As TreeView) As DataTable
    3.         Dim dt As New DataTable("TreeNodes")
    4.         Dim id As Integer = 1
    5.  
    6.         dt.Columns.Add("ParentID", GetType(Integer))
    7.         dt.Columns.Add("NodeID", GetType(Integer))
    8.         dt.Columns.Add("Name", GetType(String))
    9.  
    10.         For Each pn As TreeNode In tv.Nodes
    11.             dt.Rows.Add(0, id, pn.Name)
    12.             Me.GetAllNodes(dt, pn, id, id)
    13.             id += 1
    14.         Next
    15.  
    16.         Return dt
    17.     End Function
    18.  
    19.     Public Function FromTable(ByVal dt As DataTable, ByVal parent As String, ByVal node As String, ByVal name As String) As TreeNode()
    20.         Dim tnodes As New List(Of TreeNode)()
    21.         Dim columns As String() = New String(2) {parent, node, name}
    22.  
    23.         If dt.Columns.Contains(parent) AndAlso dt.Columns.Contains(node) AndAlso dt.Columns.Contains(name) Then
    24.             dt.DefaultView.RowFilter = "[" & parent & "] = 0"
    25.             For Each r As DataRowView In dt.DefaultView
    26.                 tnodes.Add(Me.ReadNodes(columns, r, dt))
    27.             Next
    28.             Return tnodes.ToArray()
    29.         Else
    30.             Return tnodes.ToArray()
    31.         End If
    32.     End Function
    33.  
    34.     Private Function ReadNodes(ByVal columns As String(), ByVal r As DataRowView, ByVal dt As DataTable) As TreeNode
    35.         Dim cn As New TreeNode()
    36.  
    37.         cn.Name = r(columns(2)).ToString()
    38.         cn.Text = cn.Name
    39.  
    40.         Me.GetAllNodes(columns, cn, dt, CInt(r(columns(1))))
    41.         Return cn
    42.     End Function
    43.  
    44.     Private Sub GetAllNodes(ByRef dt As DataTable, ByVal pn As TreeNode, ByVal parentid As Integer, ByRef id As Integer)
    45.         For Each cn As TreeNode In pn.Nodes
    46.             id += 1
    47.             dt.Rows.Add(parentid, id, cn.Name)
    48.             Me.GetAllNodes(dt, cn, id, id)
    49.         Next
    50.     End Sub
    51.  
    52.     Private Sub GetAllNodes(ByVal columns As String(), ByRef pn As TreeNode, ByVal dt As DataTable, ByVal nodeid As Integer)
    53.         dt.DefaultView.RowFilter = ("[" & columns(0).ToString() & "] = ") + nodeid.ToString()
    54.  
    55.         For Each r As DataRowView In dt.DefaultView
    56.             Dim cn As New TreeNode()
    57.  
    58.             cn.Name = r(columns(2)).ToString()
    59.             cn.Text = cn.Name
    60.  
    61.             pn.Nodes.Add(cn)
    62.             Me.GetAllNodes(columns, cn, dt, CInt(r(columns(1))))
    63.         Next
    64.     End Sub
    65. End Class
    Last edited by ForumAccount; Apr 5th, 2011 at 11:00 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.