遗忘海岸

江湖程序员 -Feiph(LM战士)

导航

非递归一次性加载分类数据到TreeViw

 

  不是经常用,发现再次用到时又要再写一遍,这次花点时间记录一下

  代码:
-------------------------------------

 private void InitView()
    {
        DataView dv = CommodityClassBLL.Query().Tables[0].DefaultView;
  
            if (dv.Count >= 1)
            {

                dv.Sort = "Code";
                string curCode = "";
                string prvCode = dv[0]["code"].ToString();
                trvClass.Nodes.Clear();
                trvClass.Nodes.Add(new TreeNode("行业分类", "0"));
                trvClass.Nodes[0].SelectAction = TreeNodeSelectAction.None;
               
                TreeNode root = new TreeNode(dv[0]["ClassName"].ToString(), dv[0]["ClassID"].ToString());
                trvClass.Nodes[0].ChildNodes.Add(root);
                root.NavigateUrl = string.Format(JsText, dv[0]["ClassName"].ToString(), dv[0]["ClassID"].ToString(), root.ValuePath);
                TreeNode prvNode = trvClass.Nodes[0].ChildNodes[0];//定位prvNode为第一个实际数据节点
                //prvNode用来寻找当前节点的父节点

                for (int i = 1; i < dv.Count; i++)
                {
                    curCode = dv[i]["Code"].ToString();
                    int classID = (int)dv[i]["ClassID"];
                    string className = dv[i]["ClassName"].ToString();
                    int len = curCode.Length - prvCode.Length;
                    if (len > 4)//表示当前节点是prvNode节点子节点的子节点
                    {
                       //那么prvNode.ChildNodes[prvNode.ChildNodes.Count-1]
                       //即是当前接点的父节点

                        prvNode = prvNode.ChildNodes[prvNode.ChildNodes.Count - 1];
                        prvCode = curCode.Substring(0, curCode.Length - 4);
                    }
                    else if (len < 4)//表示当前节点的父节点是prvNode向上的某个父节点
                    {
                        int level = Math.Abs(len / 4) + 1;
                        for (int j = 1; j <= level; j++)
                        {
                            prvNode = prvNode.Parent;//回溯
                        }
                        prvCode = curCode.Substring(0, curCode.Length - 4);
                    }

                    //(len==4)表示当前节点是前一节点的的直接子节点(参考下面的截图)
                    TreeNode node = new TreeNode(className, classID.ToString());
                    prvNode.ChildNodes.Add(node);
                   
                    node.NavigateUrl = string.Format(JsText, className, classID, node.ValuePath);

                   //标记选中的接点,并展开其父节点
                    if (classID == SelectedClassID)
                    {
                        node.Selected = true;
                        while (node.Parent != null)
                        {
                            node.Parent.Expand();
                            node = node.Parent;
                        }
 
                    }
                }
                if (trvClass.Nodes[0].ChildNodes != null)
                    trvClass.Nodes[0].Expand();

            }
   
    }

 -------------------------------

说明:
  dv里面的数据结构如

 

 

 

posted on 2008-09-26 14:31  遗忘海岸  阅读(802)  评论(0编辑  收藏  举报