C# TreeView SQL示例: 图文说明
http://hi.baidu.com/kjkj911/blog/category/c%23.treeview

解决方案 »

  1.   

    你窗体项目成一个dll,数据库表设计的时候:
    id  pid  TreeView节点显示名称     窗体名称         程序集名称
    1    0      XXXXX                xxxxx   
    2    1      XXXXX               单个窗体的名称   生成的Formsdll通过这个表把TreeView填充成一个树形菜单,然后在TreeNode的击事件里面写:
    取得当前点击节点的名称和数据库节点显示的名称比较,找出对应的窗体名称值和程序集名称值
    通过反射Assembly.Load(程序集名称).CreateInstance(窗体名称)动态创建一个实例,然后将该实例Show就行了
      

  2.   

    晕,从数据库,到SQL文,再到初始化TREEVIEW都给你做好了,照着拷贝就好了!
    "单击树节点调用功能窗口"这个示例中,就没有,全是自己写的方法!
      

  3.   

    <asp:TreeView ID="TreeView1" runat="server" onclick="javascript:client_OnTreeNodeChecked();" NodeIndent="10" ShowCheckBoxes="All" ShowLines="True"> <script type="text/jscript">
            function checkParent (obj)
            {
                while(obj != null) 
                {
                    var tagName = obj.tagName.toLowerCase();
                    if(tagName == "div" && obj.id == "TreeView1")
                    {
                        return;
                    }
                    
                    if(tagName == "table")
                    {
                        var checkBox = obj.getElementsByTagName("INPUT");
                        if(checkBox.length >0)
                        { 
                            checkBox[0].checked = true;
                        }
                        obj = obj.parentElement.previousSibling;
                    }
                    else
                        obj = obj.parentElement;
                 }
            }
                        
            function client_OnTreeNodeChecked()
            {
                var obj = window.event.srcElement;
                var treeNodeFound = false;
                var checkedState;
                if (obj.tagName == "INPUT" && obj.type == "checkbox")
                {
                    var treeNode = obj;
                    checkedState = treeNode.checked;
                    
                    if(checkedState)
                    {
                        checkParent(obj);
                    }
                    
                    do
                    {
                        obj = obj.parentElement;
                    }
                    while (obj.tagName != "TABLE")
                    
                    var parentTreeLevel = obj.rows[0].cells.length;
                    var parentTreeNode = obj.rows[0].cells[0];
                    
                    var tables = obj.parentElement.getElementsByTagName("TABLE");
                    var numTables = tables.length
                    if (numTables >= 1)
                    {
                        for (i=0; i < numTables; i++)
                        {
                           if(tables[i] == obj)
                           {
                                treeNodeFound = true;
                                i++;
                                if(i == numTables)
                                {
                                    return;
                                }
                            }
                            if (treeNodeFound == true)
                            {
                                var childTreeLevel = tables[i].rows[0].cells.length;
                                if (childTreeLevel > parentTreeLevel)
                                {
                                    var cell = tables[i].rows[0].cells[childTreeLevel - 1];
                                    var inputs = cell.getElementsByTagName("INPUT");
                                    inputs[0].checked = checkedState;
                                }
                                else
                                {
                                    return;
                                }
                            }
                        }
                    }
                }
            }        </script>
    void TreeBind()
        {
            TreeView1.Nodes.Clear();
            DataTable dt=BDB.GetTab("select * from Menu order by SortID");
            CreateNode(dt,TreeView1,null,0);
        }    void CreateNode(DataTable soure, TreeView tree, TreeNode parentNode, int parentId)
        {
            DataRow[] rows = soure.Select("ParentID=" + parentId.ToString());        foreach (DataRow r in rows)
            {
                TreeNode node = new TreeNode();
                node.Text = r["Text"].ToString();
                node.Value = r["ID"].ToString();
                node.SelectAction = System.Web.UI.WebControls.TreeNodeSelectAction.None;
                //node.NavigateUrl = "javascript:void(0);";            if (tree != null)
                {
                    tree.Nodes.Add(node);
                }
                else
                {
                    parentNode.ChildNodes.Add(node);
                }
                CreateNode(soure, null, node, int.Parse(r["ID"].ToString()));
            }
        }
      

  4.   


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace MyTest
    {
        public partial class Form1 : Form
        {
            public Form1() {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e) {
                TreeNode node;
                for (int i = 0; i < 10; i++) {
                    node = new TreeNode("node" + i);
                    if (i % 3 == 0) {
                        node.Nodes.Add("子节点一");
                        node.Nodes.Add("子节点二");
                        node.Nodes.Add("子节点三");
                    }
                    this.treeView1.Nodes.Add(node);            }        }        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) {
                this.richTextBox1.Text = e.Node.Text+ ": 你选中了我,从此你就是我的啦! 走,跟我回家吧!呵呵~";
                if (e.Node.Parent == null) {
                    this.richTextBox1.Text += "\r\n你是父亲节点,你有"+e.Node.Nodes.Count+"子节点";
                }
                else {
                    this.richTextBox1.Text += "\r\n你是子节点,你的父节点是" + e.Node.Parent.Text;
                    }
            }
        }
    }namespace MyTest
    {
        partial class Form1
        {
            /// <summary>
            /// 必需的设计器变量。
            /// </summary>
            private System.ComponentModel.IContainer components = null;        /// <summary>
            /// 清理所有正在使用的资源。
            /// </summary>
            /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
            protected override void Dispose(bool disposing) {
                if (disposing && (components != null)) {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }        #region Windows 窗体设计器生成的代码        /// <summary>
            /// 设计器支持所需的方法 - 不要
            /// 使用代码编辑器修改此方法的内容。
            /// </summary>
            private void InitializeComponent() {
                this.groupBox1 = new System.Windows.Forms.GroupBox();
                this.treeView1 = new System.Windows.Forms.TreeView();
                this.groupBox2 = new System.Windows.Forms.GroupBox();
                this.richTextBox1 = new System.Windows.Forms.RichTextBox();
                this.groupBox1.SuspendLayout();
                this.groupBox2.SuspendLayout();
                this.SuspendLayout();
                // 
                // groupBox1
                // 
                this.groupBox1.Controls.Add(this.treeView1);
                this.groupBox1.Location = new System.Drawing.Point(12, 12);
                this.groupBox1.Name = "groupBox1";
                this.groupBox1.Size = new System.Drawing.Size(200, 351);
                this.groupBox1.TabIndex = 0;
                this.groupBox1.TabStop = false;
                // 
                // treeView1
                // 
                this.treeView1.Location = new System.Drawing.Point(6, 20);
                this.treeView1.Name = "treeView1";
                this.treeView1.Size = new System.Drawing.Size(188, 325);
                this.treeView1.TabIndex = 0;
                this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect);
                // 
                // groupBox2
                // 
                this.groupBox2.Controls.Add(this.richTextBox1);
                this.groupBox2.Location = new System.Drawing.Point(232, 12);
                this.groupBox2.Name = "groupBox2";
                this.groupBox2.Size = new System.Drawing.Size(430, 351);
                this.groupBox2.TabIndex = 1;
                this.groupBox2.TabStop = false;
                // 
                // richTextBox1
                // 
                this.richTextBox1.Location = new System.Drawing.Point(6, 10);
                this.richTextBox1.Name = "richTextBox1";
                this.richTextBox1.Size = new System.Drawing.Size(424, 335);
                this.richTextBox1.TabIndex = 0;
                this.richTextBox1.Text = "";
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(664, 375);
                this.Controls.Add(this.groupBox2);
                this.Controls.Add(this.groupBox1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.Load += new System.EventHandler(this.Form1_Load);
                this.groupBox1.ResumeLayout(false);
                this.groupBox2.ResumeLayout(false);
                this.ResumeLayout(false);        }        #endregion        private System.Windows.Forms.GroupBox groupBox1;
            private System.Windows.Forms.TreeView treeView1;
            private System.Windows.Forms.GroupBox groupBox2;
            private System.Windows.Forms.RichTextBox richTextBox1;
        }
    }
    也许对你有用