论坛首页 入门技术论坛

菜单的递归算法

浏览 2295 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-12-29  
#region Select(无限级分类列表)
        public static List<ClassModel> Select()
        {
            DataTable dt = GetTable();
            List<ClassModel> list = new List<ClassModel>();
            ClassModel model = new ClassModel();
            model.id = 0;
            model.name = "===请选择类别===";
            list.Add(model);
            OrderSelect(dt, list, 0, "|-");
            dt.Clear();
            return list;
        }

        static DataTable GetTable()
        {
            DataTable dt = new DataTable();
            DataSet ds = new DataSet();
            Database.RunProc("CLASS_Select", out ds);
            dt = ds.Tables[0];
            return dt;
        }

        static void OrderSelect(DataTable dt, List<ClassModel> list, int classid, string strTop)
        {
            DataRow[] rows = dt.Select("classid=" + classid);
            for (int i = 0; i < rows.Length; i++)
            {
                ClassModel model = new ClassModel();
                model.id = int.Parse(rows[i]["id"].ToString());
                model.name = strTop + rows[i]["name"].ToString();
                list.Add(model);
                string str = "  " + strTop;
                int id = int.Parse(rows[i]["id"].ToString());
                OrderSelect(dt, list, id, str);
            }
        }
#endregion
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics