id=1,pid=0,广东省,
id=2,pid=0,湖南省,
id=3,pid=1,广州市,
id=4,pid=3,越秀区,
id=5,pid=4,矿泉街,
id=6,pid=5,啥啥楼盘,
id=7,pid=2,长沙市,
id=8,pid=1,佛山市,实体类-city
属性
id,pid,name,Dictionary<city, city> dict;我想把他放进字典里..Dictionary<city, city> dict;就像    
                    -矿泉街
             -越秀区
      -广州市
广东省
      -佛山市
      
      -长沙市
湖南省如何实现..脑袋有点乱

解决方案 »

  1.   


        public class city
            {            public city()
                {
                    _dict = new Dictionary<city, city>();
                }
                Dictionary<city, city> _dict;            public Dictionary<city, city> Dict
                {
                    get { return _dict; }
                    set { _dict = value; }
                }            private int _id;            public int Id
                {
                    get { return _id; }
                    set { _id = value; }
                }
                private int _pid;            public int Pid
                {
                    get { return _pid; }
                    set { _pid = value; }
                }
                private string _name;
                public string Name
                {
                    get { return _name; }
                    set { _name = value; }
                }
            }        Dictionary<city, city> dict = new Dictionary<city, city>();
            static city zi = new city();
            static city fu = zi;
            static string str = "id=1,pid=0,name=广东省,id=3,pid=1,name=广州市,id=2,pid=0,name=湖南省,id=4,pid=1,name=佛山市,id=5,pid=2,name=长沙市,id=6,pid=0,name=江苏省,id=7,pid=3,name=越秀区";
            MatchCollection matchcoll = Regex.Matches(str, @"id=([\d]+),pid=([\d]+),name=([\w]+)");
          
            public void gege(int pid)
            {   
                foreach (Match m in matchcoll)
                {
                      
                    int temp_id = Convert.ToInt32(m.Groups[1].Value);
                    int temp_pid = Convert.ToInt32(m.Groups[2].Value);
                    string temp_name = m.Groups[3].Value;
                    Boolean b = true;                //传进来的id循环对比.
                    foreach (Match c in matchcoll)
                    {
                        //如果有pid跟传入的id相等,则说明不是最后一层
                        if (c.Groups[2].Value.Equals(pid.ToString()))
                        {
                            b = false;
                            break;
                        }
                    }                if (temp_pid == pid)
                    { 
                        city c = new city();
                        c.Id = temp_id;
                        c.Pid = temp_pid;
                        c.Name = temp_name;
                        zi.Dict.Add(c, new city());
                        zi = zi.Dict[c];
                        
                        if (b)//说明已经到了最后一层了
                        {
                            //这个地方怎么写?比如读完广东省-广州市-越秀区 -****,
                            //然后我需要再指向到广东省那去..看看他还有没有其他的市..
                            //如果没有再查其他省..到这里脑袋就卡住了..有点昏
                        }
                        gege(temp_id);
                    }     
                }
            }
    这是我自己的思路...感觉好凌乱..
      

  2.   

    给个例子你。。   private void bindTvType()
            {
                tvType.Nodes.Clear();
                category[] cats = CategoryWS.getAllCategory();
                if (cats != null)
                {
                    foreach (category item in cats)
                    {
                        TreeNode node = new TreeNode();
                        if (!item.parentIdSpecified)
                        {
                            node.Text = item.name;
                            node.Expand();
                            node.Text = item.name; 
                            node.Tag = item;
                            bindChild(cats, node);
                            tvType.Nodes.Add(node);
                        }
                    }
                }
                tvType.HideSelection = false;
            }        private void bindChild(category[] cats, TreeNode tn)
            {
                foreach (category item in cats)
                {
                    TreeNode node = new TreeNode();
                    if (item.parentId == int.Parse(tn.Name))
                    {
                        node.Text = item.name;
                        node.Tag = item;
                        tn.Nodes.Add(node);
                        bindChild(cats, node);
                    }            }        }
      

  3.   

    模仿treeview的结构,实现treenode有多个子treenode。
      

  4.   


    id=1,pid=0,广东省,  =>001
    id=2,pid=0,湖南省,  =>002
    id=3,pid=1,广州市,  =>001001
    id=4,pid=3,越秀区,  =>001001001
    id=5,pid=4,矿泉街,  =>001001001001
    id=6,pid=5,啥啥楼盘,=>001001001001001
    id=7,pid=2,长沙市,  =>002001
    id=8,pid=1,佛山市,  =>001002这样的方式有利于查询, SQL只要 where tree_id like '%@tree_id' 即可查询出所有下级项.