Microsoft KB Archive/320755

= HOW TO: Populate a TreeView Control from a Dataset in Visual Basic .NET =

Article ID: 320755

Article Last Modified on 10/31/2003

-

APPLIES TO


 * Microsoft ADO.NET 1.1
 * Microsoft ADO.NET 1.0

-



This article was previously published under Q320755



IN THIS TASK
SUMMARY
 * Requirements
 * Create the Project and Add Code

REFERENCES



SUMMARY
This step-by-step article describes how to display data from a hierarchical dataset by using the TreeView control in Microsoft Visual Basic .NET. To do this, you establish a parent, child, and grandchild relationship, and then fill the TreeView control appropriately. The TreeView control has a Nodes collection with root TreeNode objects. Each TreeNode object has its own Nodes collection that holds more than one child TreeNode object. You can then use the TreeView control to work down from the parent or root node to the corresponding child nodes and display the appropriate data.

This example creates a hierarchical dataset, loops through each table, and adds nodes to the TreeView control for each child table in the dataset.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that are required:
 * Microsoft Visual Studio .NET installed on a compatible Microsoft Windows operating system
 * A SQL server with the sample Northwind database installed

This article assumes that you are familiar with the following topics:
 * Visual Basic .NET
 * ADO.NET data access

back to the top

Create the Project and Add Code
 Start Visual Studio .NET, and then create a new Visual Basic Windows Application project.  Add a reference to System.Data.SQLClient by adding the following line to the top of your Form1.vb file: Imports System.Data.SqlClient  Drag a TreeView control (Treeview1) from the toolbox to your form (Form1).  Open the form's code window, and then paste the following code into the Form1_Load event:

Note You must replace USER ID and PASSWORD with the correct values. Make sure that the USER ID has the appropriate permissions to perform these operations on the database.

Dim DSNWind As DataSet Dim CNnwind As New SqlClient.SqlConnection(&quot;DATA SOURCE=servername;USER ID= ;PASSWORD= ;INITIAL CATALOG=northwind;&quot;) '<==== CHANGE HERE Dim DACustomers As New SqlClient.SqlDataAdapter(&quot;SELECT CustomerID, CompanyName, ContactName, Country FROM customers WHERE country = 'Germany'&quot;, CNnwind) Dim DAOrders As New SqlClient.SqlDataAdapter(&quot;SELECT CustomerID, OrderID, OrderDate, ShippedDate, ShipVia, Freight FROM orders where customerid in (select customerid from customers where country = 'Germany')&quot;, CNnwind) Dim DAOrderDetails As New SqlClient.SqlDataAdapter(&quot;Select * from [Order Details] where OrderID in (SELECT OrderID FROM orders where customerid in (select customerid from customers where country = 'Germany'))&quot;, CNnwind)

DSNWind = New DataSet CNnwind.Open DACustomers.Fill(DSNWind, &quot;dtCustomers&quot;) DAOrders.Fill(DSNWind, &quot;dtOrders&quot;) DAOrderDetails.Fill(DSNWind, &quot;dtOrderDetails&quot;) 'Close the connection to the data store; free up the resources CNnwind.Close

'Create a data relation object to facilitate the relationship between the Customers and Orders data tables. DSNWind.Relations.Add(&quot;CustToOrd&quot;, DSNWind.Tables(&quot;dtCustomers&quot;).Columns(&quot;CustomerID&quot;), DSNWind.Tables(&quot;dtOrders&quot;).Columns(&quot;CustomerID&quot;)) DSNWind.Relations.Add(&quot;OrdToDet&quot;, DSNWind.Tables(&quot;dtOrders&quot;).Columns(&quot;OrderID&quot;), DSNWind.Tables(&quot;dtOrderdetails&quot;).Columns(&quot;OrderID&quot;)) '''       TreeView1.Nodes.Clear Dim i, n As Integer Dim parentrow As DataRow Dim ParentTable As DataTable ParentTable = DSNWind.Tables(&quot;dtCustomers&quot;) For Each parentrow In ParentTable.Rows Dim parentnode As TreeNode parentnode = New TreeNode(parentrow.Item(0)) TreeView1.Nodes.Add(parentnode) 'populate child'' '''           Dim childrow As DataRow Dim childnode As TreeNode childnode = New TreeNode For Each childrow In parentrow.GetChildRows(&quot;CustToOrd&quot;) childnode = parentnode.Nodes.Add(childrow(0) & &quot; &quot; & childrow(1) & &quot; &quot; & childrow(2)) childnode.Tag = childrow(&quot;OrderID&quot;) 'populate child2'                Dim childrow2 As DataRow Dim childnode2 As TreeNode childnode2 = New TreeNode For Each childrow2 In childrow.GetChildRows(&quot;OrdToDet&quot;) childnode2 = childnode.Nodes.Add(childrow2(0))

Next childrow2 

Next childrow '       Next parentrow  Modify the connection string point to point to the Northwind database on your SQL server. Press CTRL+F5 to run the program.</ol>

back to the top

<div class="references_section">