我数据库里有一张表,结构内容如下:
id menuText menuID Rootid Nodeid Role_Live
1  个人信息   1       1              4menuText(目录文本) 
menuID(目录级别) 
Rootid(一级目录ID) 
Nodeid(二级目录ID)
Role_Live(角色)我现在连接数据库没问题,从数据库里取出这个字符串也没问题,
我现在取到数据库内容,把它赋给一个字符串以后我不就知道
怎么来绑定到TreeView上了。
string ss="1 ,个人信息,1,1,, 4";主要是怎么把内容绑定到TreeView上!初学C#请各位师哥帮忙!谢谢!

解决方案 »

  1.   

    //递归函数,搜索填充树
    void FillTree(TreeNodeCollection Nodes, DataTable nodes, string parentid, int level)
    {
    for(int i=0;i<nodes.Rows.Count;i++)
    {
              
    DataRow node=nodes.Rows[i];
    if(node["Testament"].ToString().Trim().Length==0)
    continue;//如果没有定义父项,跳过
    if((int)node["Level"]!=level)//只有当前级别的项会被处理
    continue;
                if (node["Testament"].ToString() == parentid)
                {
    TreeNode treenode=new TreeNode();
                    treenode.Text =(string)node["ChineseName"];
                    ;

                    //if(!(node["Url"] is DBNull))
                    //{
                        treenode.NavigateUrl = "FrameInfo.aspx?BookID=" + node["BookId"];
                        treenode.Target = "frameSection";
                        //if(!(node["ImageUrl"] is DBNull))
                        //    treenode.ImageUrl=(string)node["ImageUrl"];
                    //}
    Nodes.Add(treenode);
                    if (Convert.ToInt32(node["Chapters"].ToString()) > 0)
                    {
                        for(int j=1;j<=Convert.ToInt32(node["Chapters"].ToString());j++)
                        {
                            TreeNode tvchild = new TreeNode("第" + j +"章", "O", null);
                            tvchild.NavigateUrl = "PagingAndSorting/CustomSortingUI.aspx?BookID=" + node["BookId"] + "&&ChapterID=" + j;
                            //tvchild.NavigateUrl = "Janus/GridEXDemo/ProgrammingSortingCS.aspx?BookID=" + node["BookId"] + "&&ChapterID=" + j;
                            tvchild.Target = "frameSection";
                            treenode.ChildNodes.Add(tvchild);
                        }
                    }
                        //continue;//如果该项没有定义NodeId,不再搜索其子项
                    FillTree(treenode.ChildNodes, nodes, node["Testament"].ToString(), level + 1);
                }
    } }这是例子,看懂就行
      

  2.   

    过段时间我将发布我自己写的XmlTreeView测试控件,用于管理树型数据,正在写使用文档和示例.
    运用此控件,您无需在数据库表中设ID,parentID
    只需在表中增加一xmltag(字段名任取)与TreeNode.Tag对应就行了
      

  3.   

    向树绑定数据,要么递归一次性绑定..
    要么分级绑定,先select出一级节点,绑定,点击展开后绑定二级..