Relational TreeView
Imports System.Data.SqlClient Imports System.Windows.Forms <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _ Partial Class Relation Inherits System.Windows.Forms.Form <System.Diagnostics.DebuggerStepThrough()> _ Private Sub InitializeComponent() Me.SplitContainer1 = New System.Windows.Forms.SplitContainer Me.treeDB = New System.Windows.Forms.TreeView Me.txtInfo = New System.Windows.Forms.TextBox Me.Panel1 = New System.Windows.Forms.Panel Me.cmdClear = New System.Windows.Forms.Button Me.cmdFill = New System.Windows.Forms.Button Me.SplitContainer1.Panel1.SuspendLayout() Me.SplitContainer1.Panel2.SuspendLayout() Me.SplitContainer1.SuspendLayout() Me.Panel1.SuspendLayout() Me.SuspendLayout() ' 'SplitContainer1 ' Me.SplitContainer1.Dock = System.Windows.Forms.DockStyle.Fill Me.SplitContainer1.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.SplitContainer1.Location = New System.Drawing.Point(5, 5) Me.SplitContainer1.Name = "SplitContainer1" ' 'SplitContainer1.Panel1 ' Me.SplitContainer1.Panel1.Controls.Add(Me.treeDB) ' 'SplitContainer1.Panel2 ' Me.SplitContainer1.Panel2.Controls.Add(Me.txtInfo) Me.SplitContainer1.Size = New System.Drawing.Size(460, 235) Me.SplitContainer1.SplitterDistance = 152 Me.SplitContainer1.TabIndex = 8 ' 'treeDB ' Me.treeDB.Dock = System.Windows.Forms.DockStyle.Fill Me.treeDB.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.treeDB.Location = New System.Drawing.Point(0, 0) Me.treeDB.Name = "treeDB" Me.treeDB.Size = New System.Drawing.Size(152, 235) Me.treeDB.TabIndex = 6 ' 'txtInfo ' Me.txtInfo.Dock = System.Windows.Forms.DockStyle.Fill Me.txtInfo.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.txtInfo.Location = New System.Drawing.Point(0, 0) Me.txtInfo.Multiline = True Me.txtInfo.Name = "txtInfo" Me.txtInfo.ReadOnly = True Me.txtInfo.Size = New System.Drawing.Size(304, 235) Me.txtInfo.TabIndex = 8 ' 'Panel1 ' Me.Panel1.Controls.Add(Me.cmdClear) Me.Panel1.Controls.Add(Me.cmdFill) Me.Panel1.Dock = System.Windows.Forms.DockStyle.Bottom Me.Panel1.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Panel1.Location = New System.Drawing.Point(5, 240) Me.Panel1.Name = "Panel1" Me.Panel1.Size = New System.Drawing.Size(460, 28) Me.Panel1.TabIndex = 7 ' 'cmdClear ' Me.cmdClear.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles) Me.cmdClear.Location = New System.Drawing.Point(386, 4) Me.cmdClear.Name = "cmdClear" Me.cmdClear.Size = New System.Drawing.Size(72, 24) Me.cmdClear.TabIndex = 4 Me.cmdClear.Text = "Clear" ' Me.cmdFill.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles) Me.cmdFill.Location = New System.Drawing.Point(4, 4) Me.cmdFill.Name = "cmdFill" Me.cmdFill.Size = New System.Drawing.Size(72, 24) Me.cmdFill.TabIndex = 3 Me.cmdFill.Text = "Fill" ' Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(470, 273) Me.Controls.Add(Me.SplitContainer1) Me.Controls.Add(Me.Panel1) Me.Name = "Relation" Me.Padding = New System.Windows.Forms.Padding(5) Me.Text = "Relation" Me.SplitContainer1.Panel1.ResumeLayout(False) Me.SplitContainer1.Panel2.ResumeLayout(False) Me.SplitContainer1.Panel2.PerformLayout() Me.SplitContainer1.ResumeLayout(False) Me.Panel1.ResumeLayout(False) Me.ResumeLayout(False) End Sub Friend WithEvents SplitContainer1 As System.Windows.Forms.SplitContainer Friend WithEvents treeDB As System.Windows.Forms.TreeView Friend WithEvents txtInfo As System.Windows.Forms.TextBox Friend WithEvents Panel1 As System.Windows.Forms.Panel Friend WithEvents cmdClear As System.Windows.Forms.Button Friend WithEvents cmdFill As System.Windows.Forms.Button Private Sub cmdFill_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFill.Click Dim Connect As String = "Data Source=localhost;Integrated Security=True;Initial Catalog=Northwind;" Dim con As New SqlConnection(Connect) Dim SQL As String = "SELECT * FROM Orders WHERE OrderDate < '2000/01/01' AND OrderDate > '1987/01/01'" Dim cmd As New SqlCommand(SQL, con) Dim adapter As New SqlDataAdapter(cmd) Dim dsNorthwind As New DataSet() con.Open() adapter.Fill(dsNorthwind, "Orders") cmd.CommandText = "SELECT * FROM Customers" adapter.Fill(dsNorthwind, "Customers") cmd.CommandText = "SELECT * FROM Employees" adapter.Fill(dsNorthwind, "Employees") con.Close() Dim relCustomersOrders As New DataRelation("CustomersOrders", _ dsNorthwind.Tables("Customers").Columns("CustomerID"), _ dsNorthwind.Tables("Orders").Columns("CustomerID")) dsNorthwind.Relations.Add(relCustomersOrders) Dim nodeParent, nodeChild As TreeNode Dim rowParent, rowChild As DataRow For Each rowParent In dsNorthwind.Tables("Customers").Rows nodeParent = treeDB.Nodes.Add(rowParent("CompanyName")) nodeParent.Tag = rowParent For Each rowChild In rowParent.GetChildRows(relCustomersOrders) nodeChild = nodeParent.Nodes.Add(rowChild("OrderID")) nodeChild.Tag = rowChild Next Next End Sub Private Sub cmdClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClear.Click treeDB.Nodes.Clear() End Sub Private Sub treeDB_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles treeDB.AfterSelect txtInfo.Text = "" Dim row As DataRow = CType(e.Node.Tag, DataRow) Dim Field As Object For Each Field In row.ItemArray txtInfo.Text &= Field.ToString & vbNewLine Next End Sub End Class