I wanted to create a dynamic tree based upon entries in a SQL database. I didn't want to modify the nodes every time I added, changed or deleted a node. The solution was to bind to a table. Below is the code you can cut-and-paste into a form and it will
work! I did this in an ASP.Net form, but I'm fairly sure it will work just as well in a Winform as well.
The format of the table is as follows (I named the table NavigationNodes):
ID ParentID Text
-- -------- ------
1 0 Parent Node1
2 1 Child1
3 1 Child2
4 1 Child3
5 4 SubChild1 of Child3
6 0 Parent Node2
7 6 Child1
etc...
The above will show as:
+ Parent Node1
Child1
Child2
+ Child3
SubChild1 of Child3
+ Parent Node2
Child1
1. Add a form to your project and drop the Infragistics UltraWebTree control on the form.
2. Paste the following line in the very top of the codebehind:
Imports System.Data.SqlClient
3. Add the following to the Page_Load:
If Not Page.IsPostBack
Then
FillTree()
End If
4. Add the following Subroutines (change the connection string accordingly):
Sub FillTree()
Dim ConnectionString
As String = "server=SQLServer;database=admin;Integrated Security=SSPI"
Dim obConn
As New SqlConnection(ConnectionString)
Dim SQL as String = "select [ID] as PGroupID, * from dbo.NavigationNodes order by ParentID Asc"
Dim obCmd As
New SqlCommand(SQL , obConn)
Dim ds As
New DataSet
Dim da As
New SqlDataAdapter(obCmd)
Dim dv As
New DataView(ds.Tables("NavigationNodes"), "", "ParentID,PGroupID", DataViewRowState.CurrentRows)
Dim n As Infragistics.WebUI.UltraWebNavigator.Node
For Each d
As DataRowView
In dv
If d.Row("ParentID") = 0
Then
n = UltraWebTree1.Nodes.Add(d.Row("Text"), d.Row("PGroupID"))
n.DataKey = d.Row("PGroupID")
AddChildNodes(n, d.Row, "X")
End Sub
Sub AddChildNodes(ByVal n
As Infragistics.WebUI.UltraWebNavigator.Node,
ByVal d As DataRow,
ByVal dataRelationName
As String)
Dim nodeChild
As New Infragistics.WebUI.UltraWebNavigator.Node
Dim childRows
As DataRow() = d.GetChildRows(dataRelationName)
For i As
Integer = 0 To childRows.Length - 1
nodeChild = n.Nodes.Add(childRows(i)("Text"), childRows(i)("PGroupID"))
nodeChild.DataKey = childRows(i)("PGroupID")
AddChildNodes(nodeChild, childRows(i), dataRelationName)
Next
End Sub
None
0 Points
4 Posts
HowTo: Flat table bound Infragistics Hierarchical Tree
Jul 20, 2005 09:39 AM|suncoast|LINK
I wanted to create a dynamic tree based upon entries in a SQL database. I didn't want to modify the nodes every time I added, changed or deleted a node. The solution was to bind to a table. Below is the code you can cut-and-paste into a form and it will work! I did this in an ASP.Net form, but I'm fairly sure it will work just as well in a Winform as well.
ID ParentID TextThe format of the table is as follows (I named the table NavigationNodes):
-- -------- ------
1 0 Parent Node1
2 1 Child1
3 1 Child2
4 1 Child3
5 4 SubChild1 of Child3
6 0 Parent Node2
7 6 Child1
etc... The above will show as: + Parent Node1
Child1
Child2
+ Child3
SubChild1 of Child3
+ Parent Node2
Child1
1. Add a form to your project and drop the Infragistics UltraWebTree control on the form.
2. Paste the following line in the very top of the codebehind:
Imports System.Data.SqlClient
3. Add the following to the Page_Load: If Not Page.IsPostBack Then
FillTree()
End If
4. Add the following Subroutines (change the connection string accordingly):
Sub FillTree() Dim ConnectionString As String = "server=SQLServer;database=admin;Integrated Security=SSPI"
Dim obConn As New SqlConnection(ConnectionString)
Dim SQL as String = "select [ID] as PGroupID, * from dbo.NavigationNodes order by ParentID Asc"
Dim obCmd As New SqlCommand(SQL , obConn) Dim ds As New DataSet
Dim da As New SqlDataAdapter(obCmd)
obCmd.Connection.Open()
da.Fill(ds, "NavigationNodes")
obCmd = Nothing
da = Nothing
obConn.Close()
obConn = Nothing
ds.Relations.Add("X", ds.Tables("NavigationNodes").Columns("PGroupID"), ds.Tables("NavigationNodes").Columns("ParentID"), False)
Dim dv As New DataView(ds.Tables("NavigationNodes"), "", "ParentID,PGroupID", DataViewRowState.CurrentRows)
Dim n As Infragistics.WebUI.UltraWebNavigator.Node
For Each d As DataRowView In dv
If d.Row("ParentID") = 0 Then
n = UltraWebTree1.Nodes.Add(d.Row("Text"), d.Row("PGroupID"))
n.DataKey = d.Row("PGroupID")
AddChildNodes(n, d.Row, "X")
Else
Exit For
End If
Next
UltraWebTree1.ExpandAll()
UltraWebTree1.SelectedNode = UltraWebTree1.Nodes(0)
End Sub Sub AddChildNodes(ByVal n As Infragistics.WebUI.UltraWebNavigator.Node, ByVal d As DataRow, ByVal dataRelationName As String) Dim nodeChild As New Infragistics.WebUI.UltraWebNavigator.NodeDim childRows As DataRow() = d.GetChildRows(dataRelationName) For i As Integer = 0 To childRows.Length - 1
nodeChild = n.Nodes.Add(childRows(i)("Text"), childRows(i)("PGroupID"))
nodeChild.DataKey = childRows(i)("PGroupID")
AddChildNodes(nodeChild, childRows(i), dataRelationName)
Next End Sub
Well, that should be it.
Enjoy!!!