62,269
社区成员
发帖
与我相关
我的任务
分享表结构如下
ID Title ParentID ChildNum Depth OrderNo
1 产品分类 0 10 0
62 动力设别 1 15 1
63 气压,液压,电气 1 3 1
65 动力设别1 62 2
66 动力设别2 62 2
67 动力设别3 62 2
68 其它 62 2
69 气压1 63 2
70 起重机及配件 1 1
在页面的表现形式是:
1 ⊙分类名称 0
2 ├动力设别 1
42│ ┌动力设别1 9
43│ ├动力设别2 10
44│ ├动力设别2 11
51│ └其他 18
14├气压,液压,电气 5
15│ ├气压1 10
15└起重机及配件
一定要用DataTable
static DataColumn column = new DataColumn();
static DataTable table = new DataTable();
static DataRow MyRow;
public static DataTable GetCategoryTree(WebInfoBase info)
{
DataTable dt = info.List(_DefaultDB, "", "[ID],[Title],[ParentID],[ChildNum],[Depth],[OrderNo]", "&version=", "[ID] ASC");
if (table.Rows.Count > 0)
{
table.Columns.Clear();
table.Rows.Clear();
}
CreateDataTable();
GetTree(dt, "0", 0);
return table;
}
public static void GetTree(DataTable dt, string pid, int blank)
{
string str = " ";
DataView dv = new DataView(dt);
dv.RowFilter = "ParentID = " + pid;
if (blank > 0)
{
string s = "";
if (blank == 1)
{
str = "├";
}
for (int i = 2; i <= blank; i++)
{
s = s + " | "+" "+" - ";
}
str = s + "├";
}
foreach (DataRowView drv in dv)
{
string id = drv["ID"].ToString();
string Title = drv["Title"].ToString();
string OrderNo = drv["OrderNo"].ToString();
string ParentID = drv["ParentID"].ToString();
string Depth = drv["Depth"].ToString();
string ChildNum = drv["ChildNum"].ToString();
MyRow = table.NewRow();
MyRow["ID"] = int.Parse(id);
MyRow["Title"] = str + Title;
MyRow["OrderNo"] = int.Parse(OrderNo);
MyRow["ParentID"] = int.Parse(ParentID);
MyRow["Depth"] = int.Parse(Depth);
MyRow["ChildNum"] = int.Parse(ChildNum);
table.Rows.Add(MyRow);
int n = int.Parse(Depth);
//if (n <= 1)
//{
n++;
//}
GetTree(dt, id, n);
}
}
public static void CreateDataTable()
{
table.Columns.Clear();
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "ID";
table.Columns.Add(column);
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "ParentID";
table.Columns.Add(column);
column = new DataColumn();
column.DataType = Type.GetType("System.String");
column.ColumnName = "Title";
table.Columns.Add(column);
column = new DataColumn();
column.DataType = Type.GetType("System.Int32");
column.ColumnName = "ChildNum";
table.Columns.Add(column);
column = new DataColumn();
column.DataType = Type.GetType("System.Int32");
column.ColumnName = "Depth";
table.Columns.Add(column);
column = new DataColumn();
column.DataType = Type.GetType("System.Int32");
column.ColumnName = "OrderNo";
table.Columns.Add(column);
//table.Columns.Clear();
} /// <summary>
/// 把一个无限循环数据表绑定,并分层显示
/// </summary>
/// <param name="dt">需要的dt</param>
/// <param name="firstfather_id">第一项的父ID值</param>
/// <param name="childid">子ID字段名</param>
/// <param name="father_id">父ID字段名</param>
/// <param name="name">显示字段名</param>
/// <param name="needlayer">显示的层数,为负值则不限</param>
/// <param name="firstnull">为定值""</param>
public static void GetTreeHtml(ref string htmlstr, DataTable dt, string firstfather_id, string childid, string father_id, string name, int needlayer, string firstnull)
{
if (needlayer > 0 || needlayer < 0)
{
DataRow[] drs = dt.Select(father_id + "=" + firstfather_id);
for (int i = 0; i < drs.Length; i++)
{
htmlstr += firstnull + drs[i][name].ToString() + "<br>";//dv[i][childid].ToString()
string firstnull1 = "";
if (firstnull.IndexOf("|-") >= 0)
{
firstnull1 = "| " + firstnull;
}
else
{
firstnull1 = " |-";
}
GetTreeHtml(ref htmlstr, dt, drs[i][childid].ToString(), childid, father_id, name, needlayer - 1, firstnull1);
}
}
} DataTable dt= ...;
string htmlstr = "";
GetTreeHtml(ref htmlstr,dt,"0","AT_Id","AT_ParentId","AT_Name",-1,"|-");
Response.Write(htmlstr);
}
/// <summary>
/// 把一个无限循环数据表绑定,并分层显示
/// </summary>
/// <param name="dt">需要的dt</param>
/// <param name="firstfather_id">第一项的父ID值</param>
/// <param name="childid">子ID字段名</param>
/// <param name="father_id">父ID字段名</param>
/// <param name="name">显示字段名</param>
/// <param name="needlayer">显示的层数,为负值则不限</param>
/// <param name="firstnull">为定值""</param>
public static void GetTreeHtml(ref string htmlstr, DataTable dt, string firstfather_id, string childid, string father_id, string name, int needlayer, string firstnull)
{
if (needlayer > 0 || needlayer < 0)
{
DataView dv = dt.DefaultView.ToTable().DefaultView;
dv.RowFilter = father_id + "=" + firstfather_id;
for (int i = 0; i < dv.Count; i++)
{
htmlstr += firstnull + dv[i][name].ToString()+"<br>";//dv[i][childid].ToString()
string firstnull1 = "";
if (firstnull.IndexOf("|-") >= 0)
{
firstnull1 = "| " + firstnull;
}
else
{
firstnull1 = " |-";
}
GetTreeHtml(ref htmlstr, dt, dv[i][childid].ToString(), childid, father_id, name, needlayer - 1, firstnull1);
}
dv.Dispose();
}
}

