|
-
May 22nd, 2009, 06:34 PM
#1
[.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:
Public Function ToTable(ByVal tv As TreeView) As DataTable
Dim dt As New DataTable("TreeNodes")
Dim id As Integer = 1
dt.Columns.Add("ParentID", GetType(Integer))
dt.Columns.Add("NodeID", GetType(Integer))
dt.Columns.Add("Name", GetType(String))
For Each pn As TreeNode In tv.Nodes
dt.Rows.Add(0, id, pn.Name)
Me.GetAllNodes(dt, pn, id, id)
id += 1
Next
Return dt
End Function
Private Sub GetAllNodes(ByRef dt As DataTable, ByVal pn As TreeNode, ByVal parentid As Integer, ByRef id As Integer)
For Each cn As TreeNode In pn.Nodes
id += 1
dt.Rows.Add(parentid, id, cn.Name)
Me.GetAllNodes(dt, cn, id, id)
Next
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:
Public Function FromTable(ByVal dt As DataTable, ByVal parent As String, ByVal node As String, ByVal name As String) As TreeNode()
Dim tnodes As New List(Of TreeNode)()
Dim columns As String() = New String(2) {parent, node, name}
If dt.Columns.Contains(parent) AndAlso dt.Columns.Contains(node) AndAlso dt.Columns.Contains(name) Then
dt.DefaultView.RowFilter = "[" & parent & "] = 0"
For Each r As DataRowView In dt.DefaultView
tnodes.Add(Me.ReadNodes(columns, r, dt))
Next
Return tnodes.ToArray()
Else
Return tnodes.ToArray()
End If
End Function
Private Function ReadNodes(ByVal columns As String(), ByVal r As DataRowView, ByVal dt As DataTable) As TreeNode
Dim cn As New TreeNode()
cn.Name = r(columns(2)).ToString()
cn.Text = cn.Name
Me.GetAllNodes(columns, cn, dt, CInt(r(columns(1))))
Return cn
End Function
Private Sub GetAllNodes(ByVal columns As String(), ByRef pn As TreeNode, ByVal dt As DataTable, ByVal nodeid As Integer)
dt.DefaultView.RowFilter = ("[" & columns(0).ToString() & "] = ") + nodeid.ToString()
For Each r As DataRowView In dt.DefaultView
Dim cn As New TreeNode()
cn.Name = r(columns(2)).ToString()
cn.Text = cn.Name
pn.Nodes.Add(cn)
Me.GetAllNodes(columns, cn, dt, CInt(r(columns(1))))
Next
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:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim TreeViewRW As New TreeViewRW
Me.DataGridView1.DataSource = TreeViewRW.ToTable(Me.TreeView1)
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:
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim tvrw As New TreeViewRW()
Dim dt As New DataTable("TreeNodes")
dt.Columns.Add("ParentID", GetType(Integer))
dt.Columns.Add("NodeID", GetType(Integer))
dt.Columns.Add("Name", GetType(String))
dt.Rows.Add(0, 1, "Parent 1")
dt.Rows.Add(0, 2, "Parent 2")
dt.Rows.Add(1, 3, "Child 1")
dt.Rows.Add(1, 4, "Child 2")
dt.Rows.Add(3, 5, "Child 3")
dt.Rows.Add(2, 6, "Child 4")
Me.TreeView1.Nodes.AddRange(tvrw.FromTable(dt, "ParentID", "NodeID", "Name"))
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:
Public Class TreeViewRW
Public Function ToTable(ByVal tv As TreeView) As DataTable
Dim dt As New DataTable("TreeNodes")
Dim id As Integer = 1
dt.Columns.Add("ParentID", GetType(Integer))
dt.Columns.Add("NodeID", GetType(Integer))
dt.Columns.Add("Name", GetType(String))
For Each pn As TreeNode In tv.Nodes
dt.Rows.Add(0, id, pn.Name)
Me.GetAllNodes(dt, pn, id, id)
id += 1
Next
Return dt
End Function
Public Function FromTable(ByVal dt As DataTable, ByVal parent As String, ByVal node As String, ByVal name As String) As TreeNode()
Dim tnodes As New List(Of TreeNode)()
Dim columns As String() = New String(2) {parent, node, name}
If dt.Columns.Contains(parent) AndAlso dt.Columns.Contains(node) AndAlso dt.Columns.Contains(name) Then
dt.DefaultView.RowFilter = "[" & parent & "] = 0"
For Each r As DataRowView In dt.DefaultView
tnodes.Add(Me.ReadNodes(columns, r, dt))
Next
Return tnodes.ToArray()
Else
Return tnodes.ToArray()
End If
End Function
Private Function ReadNodes(ByVal columns As String(), ByVal r As DataRowView, ByVal dt As DataTable) As TreeNode
Dim cn As New TreeNode()
cn.Name = r(columns(2)).ToString()
cn.Text = cn.Name
Me.GetAllNodes(columns, cn, dt, CInt(r(columns(1))))
Return cn
End Function
Private Sub GetAllNodes(ByRef dt As DataTable, ByVal pn As TreeNode, ByVal parentid As Integer, ByRef id As Integer)
For Each cn As TreeNode In pn.Nodes
id += 1
dt.Rows.Add(parentid, id, cn.Name)
Me.GetAllNodes(dt, cn, id, id)
Next
End Sub
Private Sub GetAllNodes(ByVal columns As String(), ByRef pn As TreeNode, ByVal dt As DataTable, ByVal nodeid As Integer)
dt.DefaultView.RowFilter = ("[" & columns(0).ToString() & "] = ") + nodeid.ToString()
For Each r As DataRowView In dt.DefaultView
Dim cn As New TreeNode()
cn.Name = r(columns(2)).ToString()
cn.Text = cn.Name
pn.Nodes.Add(cn)
Me.GetAllNodes(columns, cn, dt, CInt(r(columns(1))))
Next
End Sub
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|