想問大家一個問題:就是treeview控件在CS結構中的使用。假設我有一個數據庫,其中有一個表tree,兩個字段NodeID char(30), NodeName char(50) 其中NodeID表示根節點的序號,NodeName表示節點名稱。想實現如下結構:根節點用兩位數字表示(01),子節點用4位表示(0101),子節點的子節點用6們表示(010101)...以此類推。第一個根節點序號為01,第二個為02,第三個為03...結構如下:
01
02
03
04
....
根節點01的第一子節點為0101,第二個為0102,第三個為0103......
根節點02的第一子節點為0201,第二個為0202,第三個為0203.....
根節點01的第一子節點為0301,第二個為0302,第三個為0303.....
這樣要實現無限級的延伸,我只要修改相應的字段長度,就能實現。要怎麼實現??

解决方案 »

  1.   

    这样麻烦
    做无限级树比较好的结构是
    nodetext      nodeid      parentid
    abc           1           
    dddsf         2           1
    ...
      

  2.   

    //参考
    http://www.microsoft.com/china/community/Column/30.mspx
      

  3.   

    _strText = nodename;
    _strKey  = nodeid;
    private void loadtree(DataTable _dtTree)
    {
    try
    {
    this.treeview.Nodes.Clear();
    TreeNode rootNode = new TreeNode(this._strRootText);
    rootNode.Tag = this._strRootKey;
    this.treeview.Nodes.Add(rootNode);
    DataRow[] drArray = _dtTree.Select("Convert(" + this._strKey + ",'System.String') like '" + this._strRootKey + "*' and LEN(Convert(" + this._strKey + ",'System.String')) = " + Convert.ToInt32(this._strRootKey.Length + 2) );
    rootNode.Expand();
    foreach(DataRow dr in drArray)
    {
    addChildNode(rootNode,dr[this._strText].ToString(),dr[this._strKey].ToString());
    }
    }
    catch(Exception exc)
    {

    }
    }
    private void addChildNode(TreeNode parentNode,string strTextValue,string strKeyValue)
    {
    try
    {
    TreeNode thisNode = new TreeNode(strTextValue);
    thisNode.Tag = strKeyValue;
    parentNode.Nodes.Add(thisNode) ;
    DataRow[] drArray = _dtTree.Select("Convert(" + this._strKey + ",'System.String') like '" + strKeyValue + "*' and LEN(Convert(" + this._strKey + ",'System.String')) = " + Convert.ToInt32(strKeyValue.Length + 2) );

    foreach(DataRow dr in drArray)
    {
    addChildNode(thisNode,dr[this._strText].ToString(),dr[this._strKey].ToString());
    }
    }
    catch(Exception exc)
    {



    }
    }
      

  4.   

    +甲公司
          +部门a
              -张三
          +部门b
              -李四
    +乙公司
          +部门c
              -王五
    +丙公司  比如生成这样的树,先生成这样的表,然后递归生成吧
    ID是自己的编号,PID是父节点的编号。ID PID    NAME
    1   0    甲公司
    2   0    乙公司
    3   0    丙公司
    4   1    部门a
    5   1    部门b
    6   2    部门c
    7   4    张三
    8   4    李四
    9   5    王五public void AddTree(int PID,TreeNode pNode) 
    {DataView dvTree = new DataView(this.treeTable);dvTree.RowFilter =  "[PID] = " + PID;foreach(DataRowView Row in dvTree) 
    {
    TreeNode Node=new TreeNode() ;
    if(pNode == null) 
    {    
    //添加根结点
    Node.Text = Row["Name"].ToString();
    TreeView2.Nodes.Add(Node);
    Node.Expanded=true;
    AddTree(Int32.Parse(Row["ID"].ToString()), Node);   

    else 
    {   
    //添加当前结点子结点
    Node.Text = Row["Name"].ToString();
    pNode.Nodes.Add(Node);
    Node.Expanded = true;
    AddTree(Int32.Parse(Row["ID"].ToString()),Node);     
    }
    }                   
    }
      

  5.   

    修改数据库结构把,要不然一个子节点修改一下位置,重新放到另外一个目录下,那不是整个id都改变了?
    改为: id, pid, code, name 这样的结果会好处理一点
      

  6.   

    請問damofengbo一下,DataRow[] drArray = _dtTree.Select("Convert(" + this._strKey + ",'System.String') like '" + this._strRootKey + "*' and LEN(Convert(" + this._strKey + ",'System.String')) = " + Convert.ToInt32(this._strRootKey.Length + 2) );這怎麽解?如果有一個textbox,要怎麽把它值添加到資料庫中呢?
      

  7.   

    楼主的这种思路是对的,用友的科目档案和部门档案等全是用这种方式进行存储树的,操作起来也不复杂
    我给出一个不太高效的方法
    foreach (string nodecode in codelist)
    {
    TreeNode pNode = null;
    for (int i = 0; i < nodecode.Length; i++)
    {
    pNode = this.GetNode(pNode,nodecode.Substring(i * 2, 2));
    if (pNode.Text == "")
    pNode.Text = this.GetNodeText(nodecode.Substring(0, (i+1) * 2));
    }
    public TreeNode GetNode(TreeNode pNode, string code)
    {
    if(pNode==null)
    {
    TreeNode root=new TreeNode();
    root.Tag = code;
    this.m_TreeView.Nodes.Add(root);
    return root;
    }
    foreach (TreeNode node in pNode.Nodes)
    {
    if (pNode.Tag.ToString() == code)
    {
    return pNode;
    }
    }
    TreeNode nNode = new TreeNode();
    nNode.Tag = code;
    pNode.Nodes.Add(nNode);
    return nNode;
    }
    public string GetNodeText(string nodeCode)
    {
    //参数nodeCode是你的表的主键,这个函数如何返回你应该很容易做到;
    return xxxx;
    }
    我信手写的,不过估计可以行得通,如果要更高效的,就是先取出尾结点(没后代的结点),然后在这个节点中遍历.
      

  8.   

    失误,把这段改为:
    foreach (string nodecode in codelist)
    {
    TreeNode pNode = null;
    for (int i = 0; i < nodecode.Length; i+=2)//你定好的双位,如果位不固定的话,不能用这种循环,我做的科目存储每层的位就是用户自由指定的,所以必须用自定义的循环生成方式.
    {
    pNode = this.GetNode(pNode,nodecode.Substring(i * 2, 2));
    if (pNode.Text == "")
    pNode.Text = this.GetNodeText(nodecode.Substring(0, i+2);
    }
      

  9.   

    结构定为id,name,parentid
    第一个item都有自己的父id,根可以为空,这样就形成的层次结构.
      

  10.   

    要是多个表呢,不知道这样套用还行得通么?假如我把部门和用户分开,部门表可以这样使用,那用户怎么附加到子节点上?
    在线等 [email protected]