private void FillTree(int count)
{
//检查count是否与表的行数相等,
//不相等---继续递归调用
//相等-----中止调用
if (count == ds1.Tables[0].Rows.Count) {}
TreeNode trNode = new TreeNode();
TreeNode tpNode = new TreeNode();
//判断count是否与当前表的行数相等,若不相等则继续递归调用
if (count != ds1.Tables[0].Rows.Count)
{
//检查当前count行的节点是不是已经存在于tree.nodes中
//存在----count++;filltree(count);
//不存在--添加新节点
for (int j=0; j< tvRoot.Nodes.Count; j++)
{
//节点存在,添加下一个节点
if (ds1.Tables[0].Rows[count]["department_code"].ToString() == tvRoot.Nodes[j].ID.ToString())
{
count++;
FillTree(count);
}
}
//节点不存在,先添加count的父节点,再添加count结点

//如果count的father_code为空,count节点添加到根结点
string sql3 = "select * from Com. TC_PUB_DICT_DEPARTMENT where department_code = '" + ds1.Tables[0].Rows[count]["father_code"].ToString() + "'";
if (work.Public.ExecuteSql(sql3)==0)
{
trNode.Text = ds1.Tables[0].Rows[count]["department"].ToString();
trNode.ID = ds1.Tables[0].Rows[count]["department_code"].ToString();
tvRoot.Nodes.AddAt(0,trNode);
count++;
FillTree(count);
}
//如果count的father_code不为空,先添加父节点
if(work.Public.ExecuteSql(sql3)!=0)
{
DataSet ds3 = work.Public.GetRecordSet(sql3);
for (int n=0; n<tvRoot.Nodes.Count; n++)
{
//count的父节点存在
if(ds3.Tables[0].Rows[0]["department_code"].ToString() == tvRoot.Nodes[n].ID.ToString())
{
//在这个节点下添加count行为子结点
trNode.Text = trNode.Text = ds1.Tables[0].Rows[count]["department"].ToString();
trNode.ID = ds1.Tables[0].Rows[count]["department_code"].ToString();
tvRoot.Nodes[n].Nodes.AddAt(tvRoot.Nodes.IndexOf(tvRoot.Nodes[n]),trNode);
count++;
FillTree(count);
}
}
//count的父节点不存在,先添加父节点,再添加count节点
tpNode.ID = ds3.Tables[0].Rows[0]["department_code"].ToString();
tpNode.Text = ds3.Tables[0].Rows[0]["department"].ToString();
tvRoot.Nodes.AddAt(0,tpNode);
trNode.ID = ds1.Tables[0].Rows[count]["department_code"].ToString();
trNode.Text = ds1.Tables[0].Rows[count]["department"].ToString();
tpNode.Nodes.AddAt(tpNode.Nodes.IndexOf(tpNode),trNode);
count++;
FillTree(count);
}
}

}

解决方案 »

  1.   

    在这个Com. TC_PUB_DICT_DEPARTMENT表中,有department_code,department和father_code列,father_code和department_code列是对应的,跟下面这个表类似:department_code     department      father_code
         1                  aa               8
         2                  bb               8
         3                  cc                6
         4                  dd                5
         5                   rr               4
         6                    hh              8
         7                  ww                1
         8                     we             null
      

  2.   

    http://blog.csdn.net/JasonHeung/archive/2005/04/15/348731.aspx
      

  3.   

    http://dev.csdn.net/develop/article/23/23267.shtm
      

  4.   

    思路上就有问题了...  你只传一个参数,显然不够.递归的时候需要知道老爹是谁,至少是:绑儿子(老爹,数据)
    {
    读取儿子(老爹);
    ...
    if(没有儿子了)老爹.Add(数据);else
     绑儿子(儿子,数据);}
      

  5.   

    /// <summary>
            /// 动态生成树
            /// </summary>
            /// <param name="sMenuStrip"></param>
            public void eBuildTreeView(TreeView sTreeView)
            {
                try
                {
                    if (sTreeView != null)
                    {
                        DataTable dt = fGetTable("Select menuid, menuname, menutext, parentmenuid From systemmenuinfo order by menuid asc");                    for (int i = 0; i < dt.Rows.Count; i++)
                            if (dt.Rows[i]["parentmenuid"].ToString() == "0")
                            {
                                TreeNode sMagicNode = new TreeNode();
                                sMagicNode.Name = dt.Rows[i]["menuname"].ToString();
                                sMagicNode.Text = dt.Rows[i]["menutext"].ToString();
                                sMagicNode.Tag = dt.Rows[i]["menuid"].ToString();
                                sMagicNode.Checked = false;
                                sTreeView.Nodes.Add(sMagicNode);                            eCircleSubNodes(sMagicNode);
                            }
                    }                        
                }
                finally
                {
                    sTreeView.Refresh();
                }
            }        /// <summary>
            /// 循环查找子节点
            /// </summary>
            /// <param name="sMenuItem"></param>
            private void eCircleSubNodes(TreeNode sTreeNode)
            {
                try
                {
                    string sNodeId = fGetFieldValue("Select menuid, menuname From systemmenuinfo Where menuname = '" + sTreeNode.Name + "' order by menuid asc", 0, "menuid");
                    int HaveChildsCount = fGetTable("Select parentmenuid From systemmenuinfo Where parentmenuid = '" + sNodeId + "' order by menuid asc").Rows.Count;
                    if (HaveChildsCount > 0)
                    {
                        DataTable dt = fGetTable("Select menuid, parentmenuid, menuname, menutext From systemmenuinfo Where parentmenuid = '" + sNodeId + "' order by menuid asc");
                        for (int j = 0; j < HaveChildsCount; j++)
                        {
                            TreeNode sSubNode = new TreeNode();                        sSubNode.Name = dt.Rows[j]["menuname"].ToString();
                            sSubNode.Text = dt.Rows[j]["menutext"].ToString();
                            sSubNode.Tag = dt.Rows[j]["menuid"].ToString();
                            sSubNode.Checked = false;
                            sTreeNode.Nodes.Add(sSubNode);                        eCircleSubNodes(sSubNode);
                        }
                    }
                    else
                        return;
                }
                finally
                {
                }
            }
      

  6.   

    到愚翁的blog上看看,会有帮助的,http://blog.csdn.net/knight94/archive/2006/04/14/663300.aspx
      

  7.   

    private void BindData()
    {
    string strSQL = "SELECT MenuID, MenuName, PMenuID FROM SysMenu WHERE IsValid=1";
    DataSet ds=SQLHelper.ExecuteSqlGetDataSet(SQLHelper.CONN_STRING,CommandType.Text,strSQL,null);
    ViewState["ds"]=ds;
    AddTree(0, (TreeNode)null);
    }
    private void AddTree(int PMenuID,TreeNode pNode) 
    {
    DataSet ds=(DataSet)ViewState["ds"];
    DataView dvTree=new DataView(ds.Tables[0]);
    dvTree.RowFilter="[PMenuID]=" + PMenuID; foreach(DataRowView Row in dvTree) 
    {
    TreeNode Node=new TreeNode() ;
    if(pNode==null) 
    {
    Node.Text=Row["MenuName"].ToString();
    TreeMenu.Nodes.Add(Node);
    Node.Expanded=true;
    Node.CheckBox = true;
    AddTree(Int32.Parse(Row["MenuID"].ToString()), Node); 

    else 
    {
    Node.Text=Row["MenuName"].ToString();
    pNode.Nodes.Add(Node);
    Node.Expanded=true;
    Node.CheckBox = true;
    AddTree(Int32.Parse(Row["MenuID"].ToString()),Node);
    }
    }                   
    }