code ใน VB.NET 
เกี่ยวกับ Tree กับ Data Grid กับ Combo Box 
 
Option Strict On 
Option Explicit On 
Imports System 
Imports System.Data.SqlClient 
Public Class frmtree 
 
    Private Sub frmtree_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
        Call loaddata() 
        Call addcoltocombo() 
        Call adddatatotree() 
 
    End Sub 
    Private Sub loaddata() 
        Dim mycmd As New SqlCommand 
        mycmd = fnccmddspcust() 
        mydataset = f11(mydataset, mycmd, mytables.customers) 
 
    End Sub 
    Private Sub addcoltocombo() 
        For Each myrow As DataColumn In mydataset.Tables(mytables.customers).Columns 
            cbcolumn.Items.Add(myrow.ColumnName) 
        Next 
    End Sub 
    Private Sub adddatatotree() 
        Dim mynode As New TreeNode 
        For Each myrow As DataRow In mydataset.Tables(mytables.customers).Rows 
            mynode = trvcustomers.Nodes.Add(myrow("customerid").ToString) 
            Call addorder(mynode, myrow("customerid").ToString) 
        Next 
    End Sub 
    Private Sub addorder(ByVal p1 As TreeNode, ByVal p2 As String) 
        Dim mycmd As New SqlCommand 
        mycmd = fnccmdlistorders(p2, CommandType.Text) 
        mydataset = f11(mydataset, mycmd, mytables.orders) 
        For Each myrow As DataRow In mydataset.Tables(mytables.orders).Rows 
            Dim mynode As TreeNode = Nothing 
            mynode = p1.Nodes.Add(myrow("orderid").ToString) 
        Next 
    End Sub 
    Private Sub addorderdetail(ByVal p1 As TreeNode, ByVal p2 As Integer) 
        Dim mycmd As New SqlCommand 
        mycmd = fnccmdlistorderdetails(p2, CommandType.StoredProcedure) 
        mydataset = f11(mydataset, mycmd, mytables.orders) 
        For Each myrow As DataRow In mydataset.Tables(mytables.orders).Rows 
            Dim mynode As TreeNode = Nothing 
            mynode = p1.Nodes.Add(myrow("orderid").ToString) 
        Next 
    End Sub 
 
    Private Sub trvcustomers_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles trvcustomers.AfterSelect 
        Dim myorderid As Integer = CType(e.Node.Text, Integer) 
        Dim mycmd As New SqlCommand 
        Dim mybindingsource As New BindingSource 
        mycmd = fnccmdlistorderdetails(myorderid, CommandType.StoredProcedure) 
        mydataset = f11(mydataset, mycmd, mytables.orderdetails) 
        mybindingsource.DataSource = mydataset.Tables(mytables.orderdetails) 
        gdvdetail.DataSource = mybindingsource 
    End Sub 
End Class 
 
  |