zoukankan      html  css  js  c++  java
  • C#树目录(treeView)的编写

    1,数据库(DAL层)编写

    (1)模板的编写

        public class CategoryModel{
            public int _CategoryID { get; set; }
            public string _CategoryName { get; set; }
            public string _CategoryPy { get; set; }
            public int _ParentID { get; set; }
        }
    

     (2)从数据库中获取集合

            //获取类别信息
            public List<CategoryModel> GetCategoryDal(int ParentID) {
                string str = "select * from Category where ParentID=@ParentID";
                List<CategoryModel> list = new List<CategoryModel>();
                
                SQLiteParameter[] sqlitePara = { new SQLiteParameter("ParentID",ParentID) };
                DbDataReader read= SQLiteHelper.ExecuteReader(str,sqlitePara);
                while (read.Read()) {
                    if (read.HasRows) {
                        CategoryModel model = new CategoryModel();
                        model._CategoryID = read.GetInt32(0);
                        model._CategoryName = read.GetString(1);
                        model._CategoryPy = read.GetString(2);
                        model._ParentID = read.GetInt32(3);
                        list.Add(model);
                    }
                                       
                }
                return list;
            } 

    2,(BLL)层

            //获取目录信息
            public List<CategoryModel> GetCategoryInfo(int id)
            {
                ClassDal dal = new ClassDal();
                return  dal.GetCategoryDal(id);
            }

    3,树目录的编写

    表是这样的

    (用到递归的方   private void TreeGetDate(TreeNodeCollection t,int id)

       {
                ClassBll bll = new ClassBll();//新建BLL层类主要为了调用数据库
                List<CategoryModel> list = new List<CategoryModel>();
                list = bll.GetCategoryInfo(id);
    //
    数据库的数据存在这个集合中(上面的为获取指定parentID的数据)
    /*
    1,第一次传入0;TreeGetDate(TreeView1.Nodes,0);
    则list中的数据为等ParentID为0的数据

    */
    for (int i = 0; i < list.Count; i++) { string name = list[i]._CategoryName;// int pid = list[i]._CategoryID; TreeNode tree = t.Add(name); tree.Tag = pid;
    /*
    接着获取Category的值,把值传给下一次循环
    如:下一次为132则 调用
                    TreeGetDate(tree.Nodes,132);
                    结果就会在tree的子节点上添加
    这样的子节点以此类推
    */
    tree.ImageIndex
    = 0;//显示图片 TreeGetDate(tree.Nodes,pid); } }

  • 相关阅读:
    交叉编译OpenCV的教程——基于aarch64-linux-gnu的交叉编译器
    Day01:我的Python学习之路
    将中文库导入到ARM板子中以解决中文显示乱码的教程
    Linux环境下挂载SD卡的教程
    Ubuntu下压缩与解压各种文件的命令
    Ubuntu14.04环境下Qt5.5以上版本无法输入中文的解决教程
    编程之美:队列中的最大最小值
    leetcode:Compare Version Numbers
    leetcode:Search for a Range
    csapp:无符号数可能造成的程序bug
  • 原文地址:https://www.cnblogs.com/kbqLibrary/p/4769483.html
Copyright © 2011-2022 走看看