Click here to Skip to main content
15,897,704 members
Articles / Desktop Programming / Windows Forms

DataBound TreeView Control

Rate me:
Please Sign up or sign in to vote.
4.60/5 (25 votes)
6 Jan 20056 min read 228.1K   4.9K   101  
A way to bind up a simple TreeView control.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using DataBoundTreeView;

namespace TestForm
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private DataBoundTreeView.DataBoundTreeView treeResource;
		private System.Windows.Forms.DataGrid dataGrid1;
		private System.Windows.Forms.DataGrid dataGrid2;
		private System.Windows.Forms.DataGrid dataGrid3;
		private DataBoundTreeView.DataBoundTreeView treeResource2;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.ImageList imageList1;
		private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
		private System.Data.SqlClient.SqlCommand sqlSelectCommand1;
		private System.Data.SqlClient.SqlConnection sqlConnection1;
		private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter2;
		private System.Data.SqlClient.SqlCommand sqlSelectCommand2;
		private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter3;
		private System.Data.SqlClient.SqlCommand sqlSelectCommand3;
		private System.ComponentModel.IContainer components;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
			this.treeResource = new DataBoundTreeView.DataBoundTreeView();
			this.dataGrid1 = new System.Windows.Forms.DataGrid();
			this.dataGrid2 = new System.Windows.Forms.DataGrid();
			this.dataGrid3 = new System.Windows.Forms.DataGrid();
			this.treeResource2 = new DataBoundTreeView.DataBoundTreeView();
			this.button1 = new System.Windows.Forms.Button();
			this.button2 = new System.Windows.Forms.Button();
			this.imageList1 = new System.Windows.Forms.ImageList(this.components);
			this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
			this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
			this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
			this.sqlDataAdapter2 = new System.Data.SqlClient.SqlDataAdapter();
			this.sqlSelectCommand2 = new System.Data.SqlClient.SqlCommand();
			this.sqlDataAdapter3 = new System.Data.SqlClient.SqlDataAdapter();
			this.sqlSelectCommand3 = new System.Data.SqlClient.SqlCommand();
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.dataGrid2)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.dataGrid3)).BeginInit();
			this.SuspendLayout();
			// 
			// treeResource
			// 
			this.treeResource.Location = new System.Drawing.Point(16, 16);
			this.treeResource.Name = "treeResource";
			this.treeResource.Size = new System.Drawing.Size(232, 472);
			this.treeResource.TabIndex = 0;
			// 
			// dataGrid1
			// 
			this.dataGrid1.DataMember = "";
			this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.dataGrid1.Location = new System.Drawing.Point(256, 16);
			this.dataGrid1.Name = "dataGrid1";
			this.dataGrid1.Size = new System.Drawing.Size(288, 176);
			this.dataGrid1.TabIndex = 1;
			// 
			// dataGrid2
			// 
			this.dataGrid2.DataMember = "";
			this.dataGrid2.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.dataGrid2.Location = new System.Drawing.Point(256, 200);
			this.dataGrid2.Name = "dataGrid2";
			this.dataGrid2.Size = new System.Drawing.Size(288, 144);
			this.dataGrid2.TabIndex = 2;
			// 
			// dataGrid3
			// 
			this.dataGrid3.DataMember = "";
			this.dataGrid3.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.dataGrid3.Location = new System.Drawing.Point(256, 352);
			this.dataGrid3.Name = "dataGrid3";
			this.dataGrid3.Size = new System.Drawing.Size(288, 136);
			this.dataGrid3.TabIndex = 3;
			// 
			// treeResource2
			// 
			this.treeResource2.Location = new System.Drawing.Point(552, 16);
			this.treeResource2.Name = "treeResource2";
			this.treeResource2.Size = new System.Drawing.Size(232, 472);
			this.treeResource2.TabIndex = 4;
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(16, 496);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(232, 32);
			this.button1.TabIndex = 5;
			this.button1.Text = "Load Tree";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(552, 496);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(232, 32);
			this.button2.TabIndex = 6;
			this.button2.Text = "Load Tree";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// imageList1
			// 
			this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
			this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
			this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
			// 
			// sqlDataAdapter1
			// 
			this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
			this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
																									  new System.Data.Common.DataTableMapping("Table", "Customers", new System.Data.Common.DataColumnMapping[] {
																																																				   new System.Data.Common.DataColumnMapping("CustomerID", "CustomerID"),
																																																				   new System.Data.Common.DataColumnMapping("CompanyName", "CompanyName"),
																																																				   new System.Data.Common.DataColumnMapping("ContactName", "ContactName"),
																																																				   new System.Data.Common.DataColumnMapping("ContactTitle", "ContactTitle"),
																																																				   new System.Data.Common.DataColumnMapping("Address", "Address"),
																																																				   new System.Data.Common.DataColumnMapping("City", "City"),
																																																				   new System.Data.Common.DataColumnMapping("Region", "Region"),
																																																				   new System.Data.Common.DataColumnMapping("PostalCode", "PostalCode"),
																																																				   new System.Data.Common.DataColumnMapping("Country", "Country"),
																																																				   new System.Data.Common.DataColumnMapping("Phone", "Phone"),
																																																				   new System.Data.Common.DataColumnMapping("Fax", "Fax")})});
			// 
			// sqlSelectCommand1
			// 
			this.sqlSelectCommand1.CommandText = "SELECT CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region," +
				" PostalCode, Country, Phone, Fax FROM Customers";
			this.sqlSelectCommand1.Connection = this.sqlConnection1;
			// 
			// sqlConnection1
			// 
			this.sqlConnection1.ConnectionString = "workstation id=JEMERY;packet size=4096;integrated security=SSPI;data source=local" +
				"host;persist security info=False;initial catalog=Northwind";
			// 
			// sqlDataAdapter2
			// 
			this.sqlDataAdapter2.SelectCommand = this.sqlSelectCommand2;
			this.sqlDataAdapter2.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
																									  new System.Data.Common.DataTableMapping("Table", "Orders", new System.Data.Common.DataColumnMapping[] {
																																																				new System.Data.Common.DataColumnMapping("OrderID", "OrderID"),
																																																				new System.Data.Common.DataColumnMapping("CustomerID", "CustomerID"),
																																																				new System.Data.Common.DataColumnMapping("EmployeeID", "EmployeeID"),
																																																				new System.Data.Common.DataColumnMapping("OrderDate", "OrderDate"),
																																																				new System.Data.Common.DataColumnMapping("RequiredDate", "RequiredDate"),
																																																				new System.Data.Common.DataColumnMapping("ShippedDate", "ShippedDate"),
																																																				new System.Data.Common.DataColumnMapping("ShipVia", "ShipVia"),
																																																				new System.Data.Common.DataColumnMapping("Freight", "Freight"),
																																																				new System.Data.Common.DataColumnMapping("ShipName", "ShipName"),
																																																				new System.Data.Common.DataColumnMapping("ShipAddress", "ShipAddress"),
																																																				new System.Data.Common.DataColumnMapping("ShipCity", "ShipCity"),
																																																				new System.Data.Common.DataColumnMapping("ShipRegion", "ShipRegion"),
																																																				new System.Data.Common.DataColumnMapping("ShipPostalCode", "ShipPostalCode"),
																																																				new System.Data.Common.DataColumnMapping("ShipCountry", "ShipCountry")})});
			// 
			// sqlSelectCommand2
			// 
			this.sqlSelectCommand2.CommandText = "SELECT OrderID, CustomerID, EmployeeID, OrderDate, RequiredDate, ShippedDate, Shi" +
				"pVia, Freight, ShipName, ShipAddress, ShipCity, ShipRegion, ShipPostalCode, Ship" +
				"Country FROM Orders";
			this.sqlSelectCommand2.Connection = this.sqlConnection1;
			// 
			// sqlDataAdapter3
			// 
			this.sqlDataAdapter3.SelectCommand = this.sqlSelectCommand3;
			this.sqlDataAdapter3.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
																									  new System.Data.Common.DataTableMapping("Table", "Order Details", new System.Data.Common.DataColumnMapping[] {
																																																					   new System.Data.Common.DataColumnMapping("OrderID", "OrderID"),
																																																					   new System.Data.Common.DataColumnMapping("ProductID", "ProductID"),
																																																					   new System.Data.Common.DataColumnMapping("UnitPrice", "UnitPrice"),
																																																					   new System.Data.Common.DataColumnMapping("Quantity", "Quantity"),
																																																					   new System.Data.Common.DataColumnMapping("Discount", "Discount")})});
			// 
			// sqlSelectCommand3
			// 
			this.sqlSelectCommand3.CommandText = "SELECT OrderID, ProductID, UnitPrice, Quantity, Discount FROM [Order Details]";
			this.sqlSelectCommand3.Connection = this.sqlConnection1;
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
			this.ClientSize = new System.Drawing.Size(792, 536);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.treeResource2);
			this.Controls.Add(this.dataGrid3);
			this.Controls.Add(this.dataGrid2);
			this.Controls.Add(this.dataGrid1);
			this.Controls.Add(this.treeResource);
			this.Name = "Form1";
			this.Text = "BoundTreeView Example";
			this.Load += new System.EventHandler(this.Form1_Load);
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.dataGrid2)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.dataGrid3)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private NorthwindDataSet ds;

		private void Form1_Load(object sender, System.EventArgs e)
		{
			ds = new NorthwindDataSet();
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			// Fill up the DataSet
			this.sqlDataAdapter1.Fill(ds, "Customers");
			this.sqlDataAdapter2.Fill(ds, "Orders");
			this.sqlDataAdapter3.Fill(ds, "OrderDetails");

			// Create an array of TableBindings that define Table Name, Value Member and Display Member
			TableBinding[] tableBindings = new TableBinding[] {
																  new TableBinding("Customers", "CustomerID", "CompanyName"), 
																  new TableBinding("Orders", "OrderID", "OrderID"), 
																  new TableBinding("OrderDetails", "ProductID", "ProductID")};


			// Setup the initial TreeView defaults
			treeResource.TreeView.HideSelection = false;
			treeResource.TreeView.ImageList = this.imageList1;
			treeResource.TreeView.ImageIndex = 0;
			treeResource.TreeView.SelectedImageIndex = 1;

			// Load up the Tree
			treeResource.LoadTree(ds, tableBindings);

			// Load up the DataGrids
			this.dataGrid1.DataSource = ds;
			this.dataGrid1.DataMember = "Customers";
			this.dataGrid2.DataSource = ds;
			this.dataGrid2.DataMember = "Customers.CustomersOrders";
			this.dataGrid3.DataSource = ds;
			this.dataGrid3.DataMember = "Customers.CustomersOrders.OrdersOrderDetails";

		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			TableBinding[] tableBindings = new TableBinding[] { 
																  new TableBinding("Customers", "CustomerID", "CompanyName"), 
																  new TableBinding("Orders", "OrderID", "OrderID"), 
																  new TableBinding("OrderDetails", "ProductID", "ProductID")};

			treeResource2.TreeView.HideSelection = false;
			treeResource2.TreeView.ImageList = this.imageList1;
			treeResource2.TreeView.ImageIndex = 0;
			treeResource2.TreeView.SelectedImageIndex = 1;

			// I turn off the events for the first tree so that loading the second
			// tree doesn't navigate to every node in the first tree.
			// You'll notice that the DataGrids do move... You could remove the DataSource
			// from them temporarily to eliminate that as well.
			treeResource.SetEvents(ds, false);
			treeResource2.LoadTree(ds, tableBindings);
			treeResource.SetEvents(ds, true);
		}

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions