码迷,mamicode.com
首页 > Windows程序 > 详细

C#树目录(treeView)的编写

时间:2015-08-29 18:46:32      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:

 

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); } }

 

C#树目录(treeView)的编写

标签:

原文地址:http://www.cnblogs.com/kbqLibrary/p/4769483.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!