using System;
using System.Data;
using System.Text;
using BTCMS.IDAL;
using BTCMS.Utility;
using BTCMS.DALFactory;
namespace BTCMS.BLL
{
/// <summary>
/// 业务逻辑类Sort 的摘要说明。
/// </summary>
public class Sort
{
private static readonly ISort dal = DataAccess.CreateSort();
public DataTable GetStortTree()
{
//创建返回的表
DataTable dtTree = new DataTable();
dtTree.Columns.Add("SID", typeof(int));
dtTree.Columns.Add("SortStr", typeof(string));
DataRow drDefalut = dtTree.NewRow();
drDefalut["SID"] = 0;
drDefalut["SortStr"] = "---根栏目---";
dtTree.Rows.Add(drDefalut);
//获取栏目表
DataTable dtAll= dal.GetAllList("SID,NameCN,FatherID,ChildsCount,Route");
int rowAllCount = dtAll.Rows.Count;
if(rowAllCount == 0)
{
return dtTree;
}
//将栏目放如数组
string[,] a = new string[rowAllCount,4];
for(int i=0;i<rowAllCount;i++)
{
DataRow dr=dtAll.Rows[i];
a[i,0]=dr["SID"].ToString();
a[i,1]=dr["FatherID"].ToString();
a[i,2]=dr["NameCN"].ToString();
a[i,3]=dr["ChildsCount"].ToString();
}
//获取根栏目数量,进入第一次循环
DataTable dtRoot = dal.GetRootList("SID");
dtTree = GetChildsTree(a,dtTree,0,0,dtRoot.Rows.Count);
return dtTree;
}
//GetStortTree子函数用于递归
private DataTable GetChildsTree(string[,] ar, DataTable dtTree, int fatherid,int depth,int childCount)
{
int added = 0;
for(int i=0;i<ar.Length/4;i++)
{
if(Convert.ToInt32(ar[i,1]) == fatherid)
{
added += 1;
string strcur = " ";
if(fatherid !=0)
{
if (depth!=1)
{
strcur = strcur+"│";
}
if(childCount == added)
strcur = strcur+"└";
else
strcur = strcur+"├";
}
DataRow dr = dtTree.NewRow();
dr["SID"] = ar[i,0];
if(fatherid == 0)
dr["SortStr"] = strcur+"○"+ar[i,2]+"";
else
dr["SortStr"] = strcur+ar[i,2];
dtTree.Rows.Add(dr);
if(Convert.ToInt32(ar[i,3])>0)
{
dtTree=GetChildsTree(ar,dtTree,Convert.ToInt32(ar[i,0]),depth+1,Convert.ToInt32(ar[i,3]));
}
}
}
return dtTree;
}
}
}
这里把记录放到数组里递归的,有点脱了裤子放屁的味道,DATATABLE不直接就是在内存里的了么.空了修改下.
最后是返回DATATABEL直接绑定到list控件上就可以了.之所以返回datatable是因为如果我要绑定到表格的话只要便利一次datatable替换下字符串就可以了.
数据库的这几个字段是必须的。FatherID上级栏目的ID,OrderID排序的编号,ChildCount子栏目数量。
效果http://www.blueidea.com/bbs/NewsDetail.asp?lp=2&id=2548181