这个我以前在cnblogs上发过,记得那是04年刚刚工作的事。
(后来退出博客园了,博客清空了,损失很多技术收藏,原创的东东)
还经常看到有些老手不知道怎么实现,寒一个
呵呵,本人算法厉害,show一哈
public
class
WebTree {
DataTable sourceData;
StringBuilder b;
string preIdField;
string curIdField;
string curNameField;
public WebTree(DataTable t, string _curid, string _preid, string _curname) {
sourceData = t.Copy();
preIdField = _preid;
curIdField = _curid;
curNameField = _curname;
b = new StringBuilder();
}
public void BindDrp(ListItemCollection items, string preId, DataTable dt) {
if (dt.Rows.Count == 0 )
return ;
// 给出两种算法。
/* for (int i = 0; i < dt.Rows.Count; i++) {
DataRow dr = dt.Rows[i];
if (dr[preIdField].ToString() == preId) {
int depth = GetDepth(dr[preIdField].ToString());
string deep = "";
if (depth == 0)
deep = "◆ ";
else {
deep += "├"; //├─┣━
for (int j = 0; j < depth - 1; j++) {
deep += "─";
}
deep += " ";
}
items.Add(new ListItem(HttpUtility.HtmlDecode(deep + dr[curNameField].ToString()), dr[curIdField].ToString()));
string bId = dr[curIdField].ToString();
dt.Rows.Remove(dt.Rows[i]);
BindDrp(items, bId, dt);
BindDrp(items, preId, dt);
}
} */
DataRow[] rs = dt.Select(preIdField + " = " + preId, " OrderNum asc " );
if (rs.Length == 0 ) return ;
DataRow dr = rs[ 0 ];
int depth = GetDepth(dr[preIdField].ToString());
string deep = "" ;
if (depth == 0 )
deep = " ◆ " ;
else {
deep += " ├ " ; // ├─┣━
for ( int j = 0 ; j < depth - 1 ; j ++ ) {
deep += " ─ " ;
}
deep += " " ;
}
items.Add( new ListItem(HttpUtility.HtmlDecode(deep + dr[curNameField].ToString()), dr[curIdField].ToString()));
string bId = dr[curIdField].ToString();
dt.Rows.Remove(dr);
BindDrp(items, bId, dt);
BindDrp(items, preId, dt);
}
public int GetDepth( string id) {
DataRow[] rows = sourceData.Select(curIdField + " = " + id);
if (rows.Length == 0 )
return 0 ;
return GetDepth(rows[ 0 ][preIdField].ToString()) + 1 ;
}
}
DataTable sourceData;
StringBuilder b;
string preIdField;
string curIdField;
string curNameField;
public WebTree(DataTable t, string _curid, string _preid, string _curname) {
sourceData = t.Copy();
preIdField = _preid;
curIdField = _curid;
curNameField = _curname;
b = new StringBuilder();
}
public void BindDrp(ListItemCollection items, string preId, DataTable dt) {
if (dt.Rows.Count == 0 )
return ;
// 给出两种算法。
/* for (int i = 0; i < dt.Rows.Count; i++) {
DataRow dr = dt.Rows[i];
if (dr[preIdField].ToString() == preId) {
int depth = GetDepth(dr[preIdField].ToString());
string deep = "";
if (depth == 0)
deep = "◆ ";
else {
deep += "├"; //├─┣━
for (int j = 0; j < depth - 1; j++) {
deep += "─";
}
deep += " ";
}
items.Add(new ListItem(HttpUtility.HtmlDecode(deep + dr[curNameField].ToString()), dr[curIdField].ToString()));
string bId = dr[curIdField].ToString();
dt.Rows.Remove(dt.Rows[i]);
BindDrp(items, bId, dt);
BindDrp(items, preId, dt);
}
} */
DataRow[] rs = dt.Select(preIdField + " = " + preId, " OrderNum asc " );
if (rs.Length == 0 ) return ;
DataRow dr = rs[ 0 ];
int depth = GetDepth(dr[preIdField].ToString());
string deep = "" ;
if (depth == 0 )
deep = " ◆ " ;
else {
deep += " ├ " ; // ├─┣━
for ( int j = 0 ; j < depth - 1 ; j ++ ) {
deep += " ─ " ;
}
deep += " " ;
}
items.Add( new ListItem(HttpUtility.HtmlDecode(deep + dr[curNameField].ToString()), dr[curIdField].ToString()));
string bId = dr[curIdField].ToString();
dt.Rows.Remove(dr);
BindDrp(items, bId, dt);
BindDrp(items, preId, dt);
}
public int GetDepth( string id) {
DataRow[] rows = sourceData.Select(curIdField + " = " + id);
if (rows.Length == 0 )
return 0 ;
return GetDepth(rows[ 0 ][preIdField].ToString()) + 1 ;
}
}
使用示例:
DataTable data
=
new
Boards().GetList(
""
);
WebTree boardTree = new WebTree(data,T_Board.BoardId,T_Board.PreId,T_Board.BoardName);
boardTree.BindDrp(drpPre.Items, " 0 " , data);
drpPre.Items.Insert( 0 , new ListItem( "" , " 0 " ));
WebTree boardTree = new WebTree(data,T_Board.BoardId,T_Board.PreId,T_Board.BoardName);
boardTree.BindDrp(drpPre.Items, " 0 " , data);
drpPre.Items.Insert( 0 , new ListItem( "" , " 0 " ));
DataTable data
=
new
UpFiles().GetCates(
""
);
WebTree cateTree = new WebTree(data, T_FileCategory.FileCateId, T_FileCategory.PreId, T_FileCategory.CateName);
cateTree.BindDrp(drpPre.Items, " 0 " , data);
WebTree cateTree = new WebTree(data, T_FileCategory.FileCateId, T_FileCategory.PreId, T_FileCategory.CateName);
cateTree.BindDrp(drpPre.Items, " 0 " , data);
效果图: