在winform 中,有一颗树,怎么样获取它的所有叶子节点??

解决方案 »

  1.   

    有一个表,保存了一些记录。
    ParentID 为主键通过ParentID 和 ChildID分级,当记录一的ParentID为“2” 并且另有三条记录 ChildID也为“2”说明他们是父子关系。
    我要读出来,写到树形控件中。但不知道怎么样写代码,不知道楼上有没有相关的资料或代码
      

  2.   

    DataRow currentRow = ...
    int currentID = currentRow["ParentID"];
    DataRow[] childRows = dataTable.Select("childID = '" + currentID.ToString() + "'");
    for(int i=0;i<childRows.length;i++)
      ParentNode.Nodes.Add(childRows[i]["caption"]);随手写的,不足的部分你补一下吧
      

  3.   

    http://www.microsoft.com/china/community/Column/30.mspxhttp://www.microsoft.com/china/community/Column/21.mspxhttp://renyu732.cnblogs.com/archive/2005/06/28/182553.html
      

  4.   

    对于存入数据库中的TreeView信息我一般采用这样的建表方式
    TreeViewID(主键)
    MyID(本身的ID)
    PID(父节点的ID)
    MyID     PID
    1        0
    101      1
    102      1
    这样查询的时候使用递归,查找MyID=1,然后是PID=1,PID=101以此类推。
      

  5.   

    private void GetLeafNode(TreeNodeCollection tc)
    {
    foreach(TreeNode TNode in tc)
    {
    if(TNode.Nodes.Count==0)
    {
    MessageBox.Show(TNode.Text.ToString()); }
    GetLeafNode(TNode.Nodes);

    }

    }
    //调用示例
                          TreeNodeCollection tc;
    tc=treeView1.Nodes;

    GetLeafNode(tc);
      

  6.   

    要保存叶子节点将MessageBox.Show(TNode.Text.ToString());改为向数据库插入数据即可
      

  7.   

    获取一颗树的所有叶子节点:
    递归遍历树的所有节点,发现该节点下没有子节点的就是了private void FindLastChildNode(TreeNode node,ArrayList list)
    {
        if(node.ChildNodes.Count == 0)
            list.Add(node);
        else
        {
            foreach(TreeNode n in node.ChildNodes)
                this.FindLastChildNode(n,list);
        }
    }调用
    ……
    ArrayList list = new ArrayList();
    foreach(TreeNode node in TreeView.ChildNodes)
        this.FindLastChildNode(node,list);//这里得到的list里就保存了树的所有“叶子”节点
    ……