//原代码如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;namespace DirectoryDemo
{
    public partial class Form1 : Form
    {
        private String LoadingKey = "57868058-716C-4D36-B48E-D840AA2C50C3";
        private TreeView treeView1;        public Form1()
        {
            InitializeComponent();
        }        private void Form1_Load(object sender, EventArgs e)
        {
            this.BuildTreeView();
        }        private void BuildTreeView()
        {
            if (this.treeView1 == null)
            {
                this.treeView1 = new TreeView();
                this.treeView1.Dock = DockStyle.Fill;
                this.Controls.Add(this.treeView1);
                this.treeView1.BeforeExpand += new TreeViewCancelEventHandler(treeView1_BeforeExpand);
            }            using (FolderBrowserDialog dialog = new FolderBrowserDialog())
            {
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    this.treeView1.BeginUpdate();
                    this.treeView1.Nodes.Clear();
                    this.BuildTreeView(this.treeView1.Nodes, dialog.SelectedPath);
                    this.treeView1.EndUpdate();
                }
            }
        }        private void BuildTreeView(TreeNodeCollection nodes, String directoryPath)
        {
            try
            {
                // 这里这了提高效率一次只取一层节的数据不递归,若要换成数据道理一样。
                foreach (String fPath in Directory.GetDirectories(directoryPath))
                {
                    DirectoryInfo directoryInfo = new DirectoryInfo(fPath);
                    DirectoryNode directoryNode = new DirectoryNode();
                    directoryNode.Text = directoryInfo.Name;
                    directoryNode.Path = fPath;
                    directoryNode.Nodes.Add(this.CreateLoadingNode()); // 添加一个特殊节点,它的存在标志它的父节点需要动态加载
                    nodes.Add(directoryNode);
                    // this.BuildTreeView(node.Nodes, fPath); // 取消递归,在 BeforeExpand 事件中添加子级节点
                }
            }
            catch(Exception e)
            {
                MessageBox.Show(e.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }        public TreeNode CreateLoadingNode()
        {
            // 创建一个特殊节点,它的存在标志它的父节点需要动态加载
            DirectoryNode directoryNode = new DirectoryNode();
            directoryNode.Type = this.LoadingKey;
            return directoryNode;
        }        private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            DirectoryNode directoryNode = (DirectoryNode)e.Node;            if (directoryNode.Nodes.Count == 1 &&
                ((DirectoryNode)directoryNode.Nodes[0]).Type == this.LoadingKey)
            {
                // 若特殊节点存在,移除它,并为它的父节点创建子节点
                directoryNode.Nodes[0].Remove();
                this.BuildTreeView(directoryNode.Nodes, directoryNode.Path);
            }
        }
    }    public class DirectoryNode : TreeNode
    {
        public String Path { get; set; }
        public String Type { get; set; }        public DirectoryNode() : base() { }
    }
}这段展开目录的代码是可以编译通过的,现在使用BeforeExpand事件提高生成数据库(庞大数据)的树型结构,就是当点击根结点,只会显示下一级的子节点,上面的目录方法就是运用这个方法,不过改来改去,不能改成数据库的树结点,请高人指点,另外插入SQL数据库的代码贴如下:if object_id('ICBOMGROUP') is not null drop table ICBOMGROUPcreate table ICBOMGROUP(FInterID int,FParentID int,FName varchar(40),FNumber varchar(40))insert ICBOMGROUP
select 1039,0,'产品','0'     union all
select 2296,0,'喷印机壳','1' union all
select 6497,0,'丝印彩盒','2' union all
select 7035,0,'外发加工','3' union all
select 1041,1039,'VGA电视盒','01' union all
select 1071,1041,'TV3488B系列','01.01' union all
select 1140,1071,'TV2188E BOM','01.01.01' union all
select 1144,1140,'普通订单','01.01.01.01' union all
select 1146,1144,'TV2188E 中文BOM','01.01.01.01.01' union all
select 1145,1140,'特殊订单','01.01.01.02' union all
select 1129,1039,'XGA电视盒','02' union all
select 1199,1129,'TV5811系列','02.02' union all
select 1212,1199,'TV5811 BOM','02.02.01' union all
select 2297,2296,'机壳-上盖','1.01' union all
select 2308,2297,'GM998','01.01.01' union all
select 2312,2308,'GM998上盖 灰色ABS/喷银色','1.01.01.01' union all
select 7039,7035,'支架组件','3.01' union all
select 7040,7039,'PD1048','3.01.01' union all
select 7172,7040,'PD1048支架组件1','3.01.01.01'
测试成功后再另加分,谢谢,很急

解决方案 »

  1.   

    你如果要从数据库获取节点的话,你应该为你的treeview绑定一个数据源,否则的话时treeview不会依据数据库来生成的。
      

  2.   

    请把你的SQL查询语句列出来……
    谢谢
      

  3.   

     private String LoadingKey = "57868058-716C-4D36-B48E-D840AA2C50C3";
            private TreeView treeView1;
            public frmMain()
            {
                InitializeComponent();
            }
          
            private void frmMain_Load(object sender, EventArgs e)
            {
                this.BuildTreeView(); 
            }         private void BuildTreeView() 
            { 
                if (this.treeView1 == null) 
                { 
                    this.treeView1 = new TreeView(); 
                    this.treeView1.Dock = DockStyle.Fill; 
                    this.Controls.Add(this.treeView1); 
                    this.treeView1.BeforeExpand += new TreeViewCancelEventHandler(treeView1_BeforeExpand); 
                }
               
                this.treeView1.BeginUpdate();
                this.treeView1.Nodes.Clear(); 
                this.BuildTreeView(this.treeView1.Nodes,0);
                this.treeView1.EndUpdate(); 
            }        private void BuildTreeView(TreeNodeCollection nodes, int pid) 
            { 
                try
                {
                    DataSet  ds= Get(pid);
                   foreach(DataRow dr in ds.Tables[0].Rows)
                    { 
                        DirectoryNode directoryNode = new DirectoryNode();
                        directoryNode.Text = dr["FName"].ToString();
                        directoryNode.Path = dr["FInterID"].ToString(); 
                        directoryNode.Nodes.Add(this.CreateLoadingNode()); 
                        nodes.Add(directoryNode); 
                    
                    } 
                } 
                catch(Exception e) 
                { 
                    MessageBox.Show(e.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); 
                } 
            }         public TreeNode CreateLoadingNode() 
            { 
                DirectoryNode directoryNode = new DirectoryNode(); 
                directoryNode.Type = this.LoadingKey; 
                return directoryNode; 
            }         private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e) 
            { 
                DirectoryNode directoryNode = (DirectoryNode)e.Node;             if (directoryNode.Nodes.Count == 1 && 
                    ((DirectoryNode)directoryNode.Nodes[0]).Type == this.LoadingKey) 
                { 
                    directoryNode.Nodes[0].Remove(); 
                    this.BuildTreeView(directoryNode.Nodes,int.Parse(directoryNode.Path)); 
                } 
            }
            public DataSet Get(int Id)
            {            using (SqlConnection conn = new SqlConnection())
                {
                    conn.ConnectionString = "Persist Security Info=False;Data Source=(local);Initial Catalog=LifeDB;User ID=sa;Password=sa";
                    string strSql = "select * from ICBOMGROUP where FParentID='" + Id + "'";
                    DataSet ds = new DataSet();
            
                    SqlDataAdapter da = new SqlDataAdapter(strSql, conn);
                    da.Fill(ds);
                    conn.Close();
                    return ds;
                }
            }
        }     public class DirectoryNode : TreeNode 
        { 
            public String Path { get; set; } 
            public String Type { get; set; }         public DirectoryNode() : base() { } 
        }
        参考
      

  4.   

    SQL数据库数据都列在那里了哈,插入的语句都写好了