Solved

How To Dynamically Load TreeView Control

Posted on 2006-06-10
2
238 Views
Last Modified: 2008-01-16
Hi,

I’m on a tight project deadline, and do not have a lot of time to look at a Web llink.  I’m using ASP.NET 2005 with VB.NET.  I need to dynamically load a TreeView control with data from two tables.  My parent table contains the years as follows:

Year
2005
2006

My child table contains the Year and Month as follows:

Year      Month      
2005      January
2005      February
2005      March
2006         Januuary
2006      February
2006      March
2006      April

The two tables are joined by year.

My TreeView control needs to look as follows:
2005
    January
    February
    March
2006
    Janurary
    February
    March
    April

Can someone provide me with code on how to do this?

Thanks,
Densie


      
0
Comment
Question by:DeniseGoodheart
2 Comments
 
LVL 12

Accepted Solution

by:
deanvanrooyen earned 500 total points
ID: 16879933
hi,

i was going to do it for you in C# but I found a url that shows exactly what do do in VB.net, you basically read both tables into a dataset , add a datarelation (joins the tables) and then manualy add the node to the control... remeber that if your table also has url links for each row / node then you can add the field to the node.NavigateUrl
http://www.15seconds.com/issue/030827.htm
this is the section
//////////////////////////////////////////////////

Database Populating

In many cases, you may want to populate your tree from dynamic data. Obtaining your values from a database allows menu and input trees to change on the fly. While there are ways to convert your database results into XML and then populate the tree with the converted data, we can skip this conversion step and fill the tree directly from database using straightforward ADO.NET.

For the database example, we'll generate a list of Suppliers and the Products each makes. The Northwind example database in SQL 2000 will be our source. Here's a view of the tree we'll create, collapsed and expanded:

 *** see article for pic  - http://www.15seconds.com/issue/030827.htm

On your Web form, drag a TreeView control over and name it TreeView1. Below is the control code. Remember to include an import for System.Data, System.Data.SqlClient, and Microsoft.Web.UI.WebControls:


Dim strConn As String = "server=.;database=Northwind;integrated security=true;"

Dim objConn As New SqlConnection(strConn)

Dim objDS As New DataSet

Dim daSuppliers As New SqlDataAdapter("SELECT CompanyName,SupplierID FROM Suppliers", objConn)

Dim daProducts As New SqlDataAdapter("SELECT ProductName, ProductID, SupplierID FROM Products", objConn)

daSuppliers.Fill(objDS, "dtSuppliers")

daProducts.Fill(objDS, "dtProducts")

objConn.Close()


objDS.Relations.Add("SuppToProd", _

    objDS.Tables("dtSuppliers").Columns("SupplierID"), _

    objDS.Tables("dtProducts").Columns("SupplierID"))

 

Dim nodeSupp, nodeProd As TreeNode

Dim rowSupp, rowProd As DataRow

 For Each rowSupp In objDS.Tables("dtSuppliers").Rows

    nodeSupp = New TreeNode

    nodeSupp.Text = rowSupp("CompanyName")

    nodeSupp.ID = rowSupp("SupplierID")

    TreeView1.Nodes.Add(nodeSupp)

     For Each rowProd In rowSupp.GetChildRows("SuppToProd")

         nodeProd = New TreeNode

         nodeProd.Text = rowProd("ProductName")

         nodeProd.ID = rowProd("ProductID")

         nodeSupp.Nodes.Add(nodeProd)

     Next

  Next


  'clean up

  objDS.Dispose()

  daSuppliers.Dispose()

  daProducts.Dispose()

  objConn.Close()

  objConn.Dispose()

Place the control code in your Page Load area. Again, remember to include an import for System.Data, System.Data.SqlClient, and Microsoft.Web.UI.WebControls.

The first three lines are standard database connection objects:


Dim strConn As String = "server=.;database=Northwind;integrated security=true;"

Dim objConn As New SqlConnection(strConn)

Dim objDS As New DataSet

The important point on the next two SQL statements is that each statement includes a common relation field. In this case, SupplierID will connect the Suppliers to their Products. We will create a separate Data Adapter for each parent child relationship. In this case, there are only two, Suppliers and Products. If there were a relation under Products, like Colors available, we would create a third Data Adapter that would include the ProductID so Colors could be tied back to Products:


Dim daSuppliers As New SqlDataAdapter("SELECT CompanyName,SupplierID FROM Suppliers", objConn)

 

Dim daProducts As New SqlDataAdapter("SELECT ProductName, ProductID, SupplierID FROM Products", objConn)

Next we fill both Data Adapters then close our connection:


daSuppliers.Fill(objDS, "dtSuppliers")

daProducts.Fill(objDS, "dtProducts")

objConn.Close()

Relation: The key to the TreeView is the relation. When a DataSet has more than one DataTable, a relation can be defined. There must be a common field in both to define the Parent Child relationship. In our case, it's the SupplierID.


objDS.Relations.Add("SuppToProd", _

    objDS.Tables("dtSuppliers").Columns("SupplierID"), _

    objDS.Tables("dtProducts").Columns("SupplierID"))

If we had our third relation of Color, there would be a second relation statement tying Products to Color.

Now we create our TreeView datatypes:


Dim nodeSupp, nodeProd As TreeNode

Dim rowSupp, rowProd As DataRow

Loop through our data sets and populate the TreeView. The ID attribute at +++ is not needed for our example to function. Including it gives a convenient method of determining your database ID values during click and check events.


For Each rowSupp In objDS.Tables("dtSuppliers").Rows

    nodeSupp = New TreeNode

    nodeSupp.Text = rowSupp("CompanyName")

    nodeSupp.ID = rowSupp("SupplierID") +++

    TreeView1.Nodes.Add(nodeSupp)

     For Each rowProd In rowSupp.GetChildRows("SuppToProd")

         nodeProd = New TreeNode

         nodeProd.Text = rowProd("ProductName")

         nodeProd.ID = rowProd("ProductID")

         nodeSupp.Nodes.Add(nodeProd)

                ***

     Next

  Next

0
 

Author Comment

by:DeniseGoodheart
ID: 16922475
Many thanks deanvanrooyen,

Denise

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now