public partial class Form1 : Form
    {
        public enum CEnum
        {
            System = 1,
            Account = 2,
            Other = 3,
        }
        public Form1()
        {
            InitializeComponent();
        }        private void Form1_Load(object sender, EventArgs e)
        {            Class1 c1 = new Class1();
            Class1 c2 = new Class1();
            Class1 c3 = new Class1();            List<Class1> list = new List<Class1>();            c1.Type = 1;
            c1.Title = "a";
            c1.Content = "a_C";
            c1.Time = "a_T";            list.Add(c1);            c2.Type = 1;
            c2.Title = "b";
            c2.Content = "b_C";
            c2.Time = "b_T";            list.Add(c2);            c3.Type = 2;
            c3.Title = "c";
            c3.Content = "c_C";
            c3.Time = "c_T";            list.Add(c3);            foreach (Class1 c in list)
            {
                if (c.Type == 1 && !isExistSystemType)
                {
                    List<Class1> listType1 = list.Where(p => p.Type == 1).ToList();
                    CreateTreeView( c, listType1, out isExistSystemType);
                }                if (c.Type == 2 && !isExistAccoutType)
                {
                    List<Class1> listType2 = list.Where(p => p.Type == 2).ToList();
                    CreateTreeView( c, listType2, out isExistAccoutType);
                }                if (c.Type == 3 && !isExistOtherType)
                {
                    List<Class1> listType3 = list.Where(p => p.Type == 3).ToList();
                    CreateTreeView( c, listType3, out isExistOtherType);
                }            }        }
        private bool isExistSystemType = false;
        private bool isExistAccoutType = false;
        private bool isExistOtherType = false;
        private void CreateTreeView( Class1 c, List<Class1> list, out bool isExistType)
        {
            isExistType = true;
            if (list.Count == 0)
            {
                return;
            }            TreeNode[] timeNodes = new TreeNode[list.Count];
            for (int i = 0; i < list.Count; i++)
            {
                timeNodes[i] = new TreeNode(list[i].Time);
            }
            TreeNode typeNode = new TreeNode(((CEnum)c.Type).ToString(), timeNodes);
            this.treeView1.Nodes.Add(typeNode);
        }这是自己写的一个例子,treeview的结构是根节点为type,type下子节点为time,time下子点为title。
现在的问题是因为type的类型固定,很好判断,可以把相同type的time都放在该type下,可是time却不是固定的,请问大家我怎么才能把相同time的title都放在该time下啊?不知道大家理解了吗?

解决方案 »

  1.   

    如何判断呢? 而且并不是不加载这个time和title,而是如果这个time有相同的,则合并到相同的time里,
    比如time1-title1和time1-title2,就应该显示为time1-title1,title2这样,如果判断跳过time1-title2的加载,那这条信息不是不显示了??
      

  2.   

    GZ,我也正在学习C#建树的一些知识
      

  3.   

    你可以在添加节点之前先调用nodes.clear()方法,这样添加的时候就可以避免重复增加
      

  4.   

    用集合啊每次加载节点的时候你就加在集合里面。然后在加载的时候你要判断是否存在。
               private void CreateTreeView( Class1 c, List<Class1> list, out bool isExistType)
            {
              ArrayList list =new AL();
                isExistType = true;
                if (list.Count == 0)
                {
                    return;
                }            TreeNode[] timeNodes = new TreeNode[list.Count];
                for (int i = 0; i < list.Count; i++)
                {
                   if(list.Contains())  return;
                    timeNodes[i] = new TreeNode(list[i].Time);
                        list.add();
                }
                TreeNode typeNode = new TreeNode(((CEnum)c.Type).ToString(), timeNodes);
                this.treeView1.Nodes.Add(typeNode);
            }手写改的。。有错的地方还请见谅。大概这样应该可以吧
      

  5.   

    public partial class Form1 : Form
        {
            public enum CEnum
            {
                System = 1,
                Account = 2,
                Other = 3,
            }        class Class1
            {
                public CEnum type;
                public string title;
                public string content;
                public string time;
            }        public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {
                List<Class1> list = new List<Class1>();
                Class1 c1 = new Class1();
                c1.type = CEnum.System;
                c1.time = "time1";
                c1.title = "title1";
                list.Add(c1);            Class1 c2 = new Class1();
                c2.type = CEnum.System;
                c2.time = "time1";
                c2.title = "title2";
                list.Add(c2);            Class1 c3 = new Class1();
                c3.type = CEnum.System;
                c3.time = "time2";
                c3.title = "title3";
                list.Add(c3);            Class1 c4 = new Class1();
                c4.type = CEnum.System;
                c4.time = "time2";
                c4.title = "title4";
                list.Add(c4);
                string[] names = Enum.GetNames(typeof(CEnum));
                for(int i = 0; i < names.Length; i++)
                {
                    TreeNode node = new TreeNode(names[i]);
                    treeView1.Nodes.Add(node);
                    List<Class1> list1 = list.Where(p => p.type == (CEnum)(i + 1)).ToList();
                    foreach (Class1 cc in list1)
                    {
                        if (node.Nodes.IndexOfKey(cc.time) == -1)
                        {
                            TreeNode node1 = node.Nodes.Add(cc.time, cc.time);
                            List<Class1> list2 = list.Where(p => p.type == (CEnum)(i + 1) && p.time == cc.time).ToList();
                            foreach (Class1 ccc in list2)
                            {
                                node1.Nodes.Add(ccc.title);
                            }
                        }
                    }
                }
            }