如果我数据库某张表内只有三行记录
id        //
name      //
parentid  //父级ID,要求是id中存在的数据请问有没有一种算法或程序做个超阶级的TreeView出来

解决方案 »

  1.   

    外网没有C#只是大概意思
    大概意思
    定义一个类或struct
    class node
    {
    string id
    string name
    string parentid
    }忽略大小写
    treeview 为TreeView对象HashTable parentlist="select * from table where parentid==null"//选择所有父结点为空的记录//////...............................其他程序
    private TreeNode tn=new TreeNode("root");
    private void loopAdd(TreeNode temptn,HashTable ht)//把parentlist负值给ht
    {
    foreach(IDictionary obj in ht)
    {
    TreeNode newNode=new TreeNode();//生成节点
    tn.Nodes.Add(newNode);//加为tn的子节点
    HashTable tempHT=new HashTable();//
    //从数据库中查询"select * from table where parentid='"+obj.id+"'"并生成node对象存入tempHT中
    if(tempHT.count>0)//有子节点
    {  loopAdd(newNode,tempHT);  }
    }
    }
    调用loopAdd后
    this.treeview.Nodes.add(tn);