LogoASPAlliance: Articles, reviews, and samples for .NET Developers
Build a TreeView Control in Less Than Two Minutes with Visual Studio 2005 Beta 2
by Steven Swafford
Average Rating: 
Views (Total / Last 10 Days): 39102/ 52


[ Download Code ]

This article will present how you can build a working TreeView control in less than two minutes, utilizing an XML file as its data source. As well, I will also cover a database data-driven approach in lieu of using an XML file. To comprehend this approach, you should be familiar with PL/SQL, SQL, Visual Studio 2005 Beta 2, and XML. I for one have felt the pain of building a TreeView in the past utilizing Active Server Pages and this feature is a welcomed addition to the Visual Studio .NET IDE, in my humble opinion.


  • Oracle 9i or better database instance
  • ODP.NET (The Oracle Data Provider for .NET)
  • PL/SQL Developer, Toad, SQLPlus or you favorite text editor
  • .NET Beta 2
  • Visual Studio .NET 2005 Beta 2


The end result that you should take away from this article is how to build a TreeView control within Visual Studio 2005 Beta 2 without writing a single line of code, by utilizing an XML file as the DataSource and the TreeView GUI properties. As well you should comprehend how to use an Oracle database and the ODP.NET provider as an alternative to the XML DataSource.

XML as the TreeView's DataSource

The simplest method of rolling out your TreeView control is utilizing the interface within Visual Studio 2005 Beta 2 without having to write one single line of code. Pretty cool huh! The first thing you will need is a well-formed XML file. For the purpose of this article, I am utilizing an XML file which contains Name and URL values.

Listing 1: authors.xml

<?xml version="1.0" standalone="yes"?>
    <Author NAME="Steven Smith">
        <blog URL=""></blog>
    <Author NAME="Robert Chartier">
        <blog URL=""></blog>
    <Author NAME="Steven Swafford">
        <blog URL=""></blog>
    <Author NAME="J. Ambrose Little">
        <blog URL=""></blog>

The first thing you need to do is to create a new Web Form. Once you have the Web Form in place, there are two controls that you must add to this Web Form. First is the XmlDataSource, which will be what we will use as the DataSource.

Figure 1: Configure Data Source
Configure Data Source

Next, drag and drop a TreeView control onto your Web Form. Bring up the TreeView Tasks and select the XmlDataSource that you previously configured. In this case, the name of the DataSource is XmlDataSource1.

Figure 2: TreeView Tasks, XmlDataSource Configuration
TreeView Tasks, XmlDataSource Configuration

The final step in this process is to open the TreeView DataBindings Editor and configure the TreeView control.

Figure 3: TreeView DataBindings Editor
TreeView DataBindings Editor

There are three items that we will databind to this TreeView control.

  1. ASPAlliance_Authors – parent node
    1. In the available data bindings area, highlight ASPAlliance_Authors and click Add. Next, within the data binding properties, add the text “ASPAlliance Authors Blog List” to the Text property.
  2. Author – child node
    1. In the available data bindings area, highlight Author and click Add. In the data bindings TextField attribute, select NAME from the dropdown list.
  3. URL – Used as an HREF that will be attached to the Author child node
    1. In the available databindings area, highlight Blog and click Add. In the data bindings TextField attribute, select URL from the dropdown list, and select URL as well for the NavigateUrlField attribute.

Finally, make sure that the "Auto-generate data bindings" checkbox is checked.

That is all it takes to generate your TreeView control, without writing one single line of code. Execute this Web Form and you will see the following.

Figure 4: TreeView Control Execution
TreeView Control Execution


Oracle Database as the TreeView's DataSource

While working with a database as the data source for your TreeView Control requires a little more effort and code than an XML data source, it is still a straight forward process. We will be working with two tables, one named EMP and the other DEPT. You are probably asking yourself, “Where are the emp and dept tables?” No fear, I have included them with the sample code download. Be sure to execute dept.sql and then emp.sql, in this order.

The first thing you must do is add a reference to ODP.NET (Oracle.DataAccess.dll) from your ORACLE_HOME. Add a new Web Form and define the strings for your database authentication and SQL statements, as well be sure to import the namespaces Client and Types.

Listing 2: SQL and Database Authentication Strings

private const string dbConnString = "Data Source=dbinstance;User ID=userid; 
private const string empQuery = "SELECT * FROM emp";
private const string deptQuery = "SELECT * FROM dept";

Note: Be sure to change dbinstance to the Oracle service you are using, as well as include the proper username and password.

Listing 3: ODP.NET Imports

// ODP.NET Import(s)
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;

Now that you have imported the Client and Types namespaces and defined your database connection string, employee query, and dept query, it is time to write the code that will query the database and populate the TreeView control. We will start with the GetData() method, since we require a DataSet to populate the actual TreeView control.

Listing 4: GetData() Method

public DataSet GetData()
    OracleConnection dbConn = new OracleConnection(dbConnString);
    OracleDataAdapter empDataAdapter = new OracleDataAdapter(empQuery, dbConn);
    OracleDataAdapter deptDataAdapter = new OracleDataAdapter(deptQuery, dbConn);

    DataSet myDataSet = new DataSet();

    empDataAdapter.Fill(myDataSet, "emp");
    deptDataAdapter.Fill(myDataSet, "dept");

    myDataSet.Relations.Add("Child", myDataSet.Tables["dept"].Columns["deptno"],

    return myDataSet;

The GetData() method performs the following:

  1. establishes a connection to the Oracle database;
  2. establishes an employee DataAdapter; and 
  3. establishes a department DataAdapter.

The one unique thing that I am accomplishing here is utilizing the Relations property of the DataSet to build a DataSet of linked tables, which in this case are the two tables named EMP and DEPT. If you are not familiar with the Relations property, you may read more at MSDN. Now that you have a DataSet populated via the EMP and DEPT tables, then joined via the Relations property of the DataSet, it is now time to populate the TreeView with the PopulateTreeView() method.

Listing 5: PopulateTreeView()

public void PopulateTreeView()
    DataSet myDataSet = GetData();
    foreach (DataRow parentRow in myDataSet.Tables["dept"].Rows)
        TreeNode parentNode = new TreeNode((string)parentRow["dname"]);
        foreach (DataRow childRow in parentRow.GetChildRows("Child"))
            TreeNode childNode = new TreeNode((string)childRow["ename"]);

As you look at the PopulateTreeView() method in Listing 5 above, you will notice that I first instantiate a DataSet named myDataSet, that in turn calls the GetData() method. The next step in this process is to loop through the rows contained within our DataSet, which will be our parent rows, and as well I have a loop inside the parent to retrieve the child rows. This is the overall process of how the TreeView is built.

The last thing to do, only for testing purposes, is to call PopulateTreeView() within the Page_Load method.

Listing 6: Example Page_Load

protected void Page_Load(object sender, EventArgs e)

Now execute the Web Form and you should see a TreeView similar to the following:

Figure 5: Rendered Web Form Containing a TreeView Control
Example Page_Load


I have demonstrated how to build a TreeView control in a matter of minutes using nothing but the Visual Studio 2005 Beta 2 IDE and an XML file as the DataSource. As well, I also covered utilizing ODP.NET and an Oracle database to bind a DataSet containing information obtained via a SQL query to the EMP and DEPT tables. While working with a database versus an XML file does require a bit more effort, I only hope I have provided a clear and concise example of accomplishing such a task. If you have ever worked with a TreeView in the past, I am sure that you will be pleasantly surprised at how easy it is to work with this control.

Feel free to discuss this article at my Blog.

©Copyright 1998-2021  |  Page Processed at 2021-12-01 8:34:48 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search