c# treeView 绑定集合

      //  public static List ConvertToTreeViewData(
        /// 
        /// 向TreeView填充数据
        /// 
        /// 
        /// 要填充的数据
        /// 最高类别的父类别
        /// 
        public static bool InsertDataToTreeView(TreeView treeView, List treeDatas,int pID) {
            TreeViewTool.pID = pID;
            int len = treeDatas.Count;
            int j = 0;
            for (int i = 0; i < len; i++) {
                if ((int)treeDatas[i].PID == pID) {
                    treeView.Nodes.Add(treeDatas[i].Name.ToString());
                    treeView.Nodes[j].Tag = treeDatas[i].Id;
                    j++;
                }
            }
            for (int i = 0; i < treeView.Nodes.Count; i++) {
                TempTreeView(treeView.Nodes[i], treeDatas);
            }
                return false;
        }

        private static void TempTreeView(TreeNode tn, List treeDatas) {
            int len = treeDatas.Count;
            int j = 0;
            for (int i = 0; i < len; i++) {
                if ((int)treeDatas[i].PID != pID) {
                    if ((int)treeDatas[i].PID == (int)tn.Tag) {
                        tn.Nodes.Add(treeDatas[i].Name.ToString());
                        tn.Nodes[j].Tag = treeDatas[i].Id;
                        TempTreeView(tn.Nodes[j], treeDatas);
                        j++;
                    }
                }
            }
        }

调用上边的第一个方法,并且里边需要用到一个实体类:

  public class TreeViewData {
         Object id;
         /// 
         /// 编号
         /// 
         public Object Id {
             get { return id; }
             set { id = value; }
         }


         Object name;
         /// 
         /// 名称
         /// 
         public Object Name {
             get { return name; }
             set { name = value; }
         }


         Object pID;
         /// 
         /// 父类别
         /// 
         public Object PID {
             get { return pID; }
             set { pID = value; }
         }


    }



你可能感兴趣的:(c#用法技巧,c#,object,class)