Search:
Namespaces
Discussions
.NET v1.1
Feedback
Treeview from database
Messages
Related Types
This message was discovered on
ASPFriends.com 'aspngwebcontrols' list
.
Greg
Anyone have examples on how to pull a treeview from a database, with the
database containing the parent and child node ID's?
Thanks
Greg
Reply to this message...
James Avery
Try this out Greg:
http://www.aspalliance.com/jamesavery/webcontrols/treeviewp2.aspx
James
-----Original Message-----
From: Greg [mailto:
Click here to reveal e-mail address
]
Sent: Friday, June 28, 2002 12:19 PM
To: aspngwebcontrols
Subject: [aspngwebcontrols] Treeview from database
Anyone have examples on how to pull a treeview from a database, with the
database containing the parent and child node ID's?
Thanks
Greg
Reply to this message...
Michael S. Hunsicker
Hi Greg,
Here is the code behind for a treeview that loads parent and child node
information from a database. It can handle any level of nesting (for the
"category" node) specified by the retrieved data. I built this for a data
structure designed specifically to deal with child-parent relationships; if
you're interested I can send you more information on the data structure.
This probably isn't the most efficient approach, but it works well and loads
pretty fast.
Hope this helps,
Mike
------------------
Private Sub Page_Load(ByVal sender As System.
Object
, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not (IsPostBack) Then
GetTreeData()
BuildTree()
End If
End Sub
Private Sub GetTreeData()
'retrieve groups
sqlCmd = New
SqlCommand
("select group_oid [id], name [desc] from
tb_group", sqlCn)
sqlDA = New
SqlDataAdapter
(sqlCmd)
sqlDA.Fill(dsGroups, "Group")
'retrieve top level categories
sqlCmd = New
SqlCommand
("sp_tb_get_top_categories", sqlCn)
sqlCmd.
CommandType
=
CommandType
.StoredProcedure
sqlDA = New
SqlDataAdapter
(sqlCmd)
sqlDA.Fill(dsTCat, "Category")
'retrieve all nested parent categories
sqlCmd = New
SqlCommand
("sp_tb_get_categories", sqlCn)
sqlCmd.
CommandType
=
CommandType
.StoredProcedure
sqlDA = New
SqlDataAdapter
(sqlCmd)
sqlDA.Fill(dsCats, "Categories")
'retrieve all published models
sqlCmd = New
SqlCommand
("sp_tb_get_models_all", sqlCn)
sqlCmd.
CommandType
=
CommandType
.StoredProcedure
sqlDA = New
SqlDataAdapter
(sqlCmd)
sqlDA.Fill(dsModels, "Models")
End Sub
Private Sub BuildTree()
' set tree properties
With tv
.EnableViewState = True
.AutoPostBack = True
.SelectExpands = True
With .DefaultStyle
.Add("font-size", "12")
.Add("font-family", "Arial")
End With
End With
' build nodes
Dim sId As String
Dim drs() As
DataRow
= dsGroups.Tables("Group").Select()
Dim dr As
DataRow
For Each dr In drs
Dim ParentNode As New
TreeNode
()
With ParentNode
.Type = "Group"
.Text = dr.Item("desc").ToString
.ID = dr.Item("id").ToString
With .DefaultStyle
.Add("width", "98%")
.Add("background-color", "#eeeeee")
.Add("border", "solid 1px #777777")
.Add("padding", "1px")
End With
End With
'add root level child nodes to group node
AddTopChild(ParentNode, dr.Item("id").ToString)
'add group node to treeview
tv.Nodes.Add(ParentNode)
Next
End Sub
Private Sub AddTopChild(ByRef ParentNode As
TreeNode
, ByVal ParentId As
String)
Dim drs As
DataRow
() = dsTCat.Tables("Category").Select()
Dim dr As
DataRow
If drs.GetLength(0) > 0 Then
For Each dr In drs
If dr.Item("parent") = ParentId Then
Dim TopChildNode As New
TreeNode
()
Dim sDesc As String = dr.Item("desc").ToString
Dim sId As String = dr.Item("id").ToString
With TopChildNode
.Type = "Category"
.Text = sDesc
.ID = sId
.DefaultStyle.Add("color", "maroon")
End With
'add models to root level child nodes
AddModels(TopChildNode, sId)
'add children to root level child nodes
AddChildren(TopChildNode, sId)
'add root level child node to group node
ParentNode.Nodes.Add(TopChildNode)
End If
Next
End If
End Sub
Private Sub AddChildren(ByRef ParentNode As
TreeNode
, ByVal sParentId As
String)
Dim drs As
DataRow
() = dsCats.Tables("Categories").Select()
Dim dr As
DataRow
If drs.GetLength(0) > 0 Then
For Each dr In drs
If dr.Item("parent") = sParentId Then
Dim ChildNode As New
TreeNode
()
Dim sDesc As String = dr.Item("desc").ToString
Dim sId As String = dr.Item("id").ToString
Dim sType As String = "Category"
With ChildNode
.Type = "Category"
.Text = sDesc
.ID = sId
.DefaultStyle.Add("color", "maroon")
End With
'add models to child node
AddModels(ChildNode, sId)
'add child to root level child node
ParentNode.Nodes.Add(ChildNode)
'call AddChildren for each child to recursively add all
children
AddChildren(ChildNode, sId)
End If
Next
End If
End Sub
Private Sub AddModels(ByRef ParentNode As
TreeNode
, ByVal sParentId As
String)
Dim drs As
DataRow
() = dsModels.Tables("Models").Select()
Dim dr As
DataRow
If drs.GetLength(0) > 0 Then
For Each dr In drs
If dr.Item("parent") = sParentId Then
Dim ModelNode As New
TreeNode
()
Dim sDesc As String = dr.Item("desc").ToString
Dim sId As String = dr.Item("id").ToString
With ModelNode
.Type = "Model"
.Text = sDesc
.ID = sId
.DefaultStyle.Add("font-style", "italic")
.ImageUrl = sImagesPath & "\addNote.gif"
End With
'add model to child node
ParentNode.Nodes.Add(ModelNode)
End If
Next
End If
End Sub
-----Original Message-----
From: Greg [mailto:
Click here to reveal e-mail address
]
Sent: Friday, June 28, 2002 11:19 AM
To: aspngwebcontrols
Subject: [aspngwebcontrols] Treeview from database
Anyone have examples on how to pull a treeview from a database, with the
database containing the parent and child node ID's?
Thanks
Greg
This e-mail and any attachments may contain privileged and/or confidential
information. This e-mail is intended solely for the use of the individual
or entity to which it is addressed. If you are not the intended recipient
of this e-mail, you are hereby notified that any copying, distribution,
dissemination or action taken in relation to the contents of this e-mail
and any of its attachments is strictly prohibited and may be unlawful. If
you have received this e-mail in error, please notify the sender
immediately and permanently delete the original e-mail and destroy any
copies or printouts of this e-mail as well as any attachments.
Reply to this message...
Murad Kayani
Hi,
i did this for my recent project.
Table Structure
Folderid Projectid FolderName ParentID
0 1 Extranet 0
1 1 Personal 0
2 1 MyDocuments 1
3 1 Images 1
4 1 Files 0
5 1 MyFiles 4
6 1 MyFiles2 4
7 1 MyImages 3
8 1 MyImages2 3
//Creating
DataSet
for
TreeView
string Qry = "Select * from folders ";
SqlDataAdapter
myCommand = new
SqlDataAdapter
(Qry ,myConnection);
DataSet
dsFolder = new
DataSet
();
myCommand.Fill(dsFolder,"Folders");
//Creating Type for Nodes
// add tree node "type" for folders and files
string imgurl = "Images/";
TreeNodeType type;
type = new TreeNodeType();
type.Type = "folder";
type.ImageUrl = imgurl + "folder.gif";
type.ExpandedImageUrl = imgurl + "folderopen.gif";
TreeView1.TreeNodeTypes.Add( type );
//RootNode is hardcoded for seting intial value to NodeData Peoperty of
treeNode.
Microsoft.Web.UI.WebControls.
TreeNode
RootNode;
RootNode = new Microsoft.Web.UI.WebControls.
TreeNode
();
RootNode.NodeData="0";
RootNode.Text= ["FolderName"].ToString();
RootNode.Type="folder";
RootNode.NavigateUrl="DocumentList.aspx?Fid="+ "0".ToString();
RootNode.Target="doc"; //If you are using Frame.
TreeView1.Nodes.Add(RootNode);
RootNode.Expanded = true;
//Using Recursive Function for looping throught Each node and find the value
of Node Data. If the Value of Node Data is equal //to Folders Parent id then
it will add new Node into that.
TreeNode
newNode;
foreach(
DataRow
Folder in
dsFolder.Tables["Folders"].Rows)
{
newNode= new
Microsoft.Web.UI.WebControls.
TreeNode
();
newNode.NodeData=
Folder["Folderid"].ToString();
newNode.Text=Folder["FolderName"].ToString();
newNode.Type="folder";
newNode.NavigateUrl =
"DocumentList.aspx?Fid=" + Folder["Folderid"];
newNode.Target= "doc";
AttachByData(newNode,Folder["Parentid"].ToString());
}
}
}
private void AttachByData(
TreeNode
node,string NodeData)
{
foreach(
TreeNode
n in TreeView1.Nodes)
{
FindRecursive(node,n,NodeData);
}
}
private void FindRecursive(
TreeNode
AttachNode,
TreeNode
n,string Data)
{
if (n.NodeData == Data)
{
n.Nodes.Add(AttachNode);
}
else
{
foreach
(Microsoft.Web.UI.WebControls.
TreeNode
aNode in n.Nodes)
{
FindRecursive(AttachNode,aNode,Data);
}
}
-----Original Message-----
From: Greg [mailto:
Click here to reveal e-mail address
]
Sent: Friday, June 28, 2002 9:19 AM
To: aspngwebcontrols
Subject: [aspngwebcontrols] Treeview from database
Anyone have examples on how to pull a treeview from a database, with the
database containing the parent and child node ID's?
Thanks
Greg
Reply to this message...
mic
Hi Murad Kayani,
I have tried to make a treeview with the code you post above.
I am working with visual studio .net 2003, but i have some build errors.
Did you visually create the sqlconnection and sqladapter?
Maybe you could upload the files somewhere so i can download it or maybe you can mail them?
--------------------------------
From: mic
Reply to this message...
System.Data.CommandType
System.Data.DataRow
System.Data.DataSet
System.Data.SqlClient.SqlCommand
System.Data.SqlClient.SqlDataAdapter
System.EventArgs
System.Object
System.Windows.Forms.TreeNode
System.Windows.Forms.TreeView
Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us
-
Terms of Use
-
Privacy Policy
-
www.dotnet247.com