PS:以前写过了的,这次整理下 namespace XR.Web { using System; using System.Collections; using System.Configuration; using System.Data; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Data.Sql; using System.Data.SqlClient; public partial class Default2 : System.Web.UI.Page { DataTable Tables = new DataTable(); DataColumn DC = new DataColumn(); DataRow DR; ProductCategoryBLL Category = new ProductCategoryBLL(); protected void Page_Load(object sender, EventArgs e) { DataTable DT = Category.GetDataTable("YLProductCategory", 0); CreateDataTable(); GetTree(DT, 0, 0); DropDownList1.DataSource = Tables; DropDownList1.DataTextField = "Title"; DropDownList1.DataValueField = "ID"; DropDownList1.DataBind(); } public void GetTree(DataTable DT,int PID,int Depth) { DataView DV = DT.DefaultView; DV.RowFilter = "ParentID=" + PID; string str = string.Empty; if (Depth == 0) { str = ""; } else if (Depth == 1) { str = "├"; } else { for (int i = 0; i < Depth; i++) { str =str+"├" + "" + "-"; } } foreach (DataRowView Drv in DV) { int ID = int.Parse(Drv["ID"].ToString()); string Title = Drv["Title"].ToString(); int ParentID = int.Parse(Drv["ParentID"].ToString()); int Dep = int.Parse(Drv["Depth"].ToString()); DR = Tables.NewRow(); DR["ID"] = ID; DR["Title"] = str + Title; DR["ParentID"] = ParentID; DR["Depth"] = Dep; Tables.Rows.Add(DR); int n = Dep; n++; GetTree(DT,ID, n); } } public void CreateDataTable() { DC = new DataColumn(); DC.ColumnName = "ID"; DC.DataType = System.Type.GetType("System.Int32"); Tables.Columns.Add(DC); DC = new DataColumn(); DC.ColumnName = "Title"; DC.DataType = System.Type.GetType("System.String"); Tables.Columns.Add(DC); DC = new DataColumn(); DC.ColumnName = "ParentID"; DC.DataType = System.Type.GetType("System.Int32"); Tables.Columns.Add(DC); DC = new DataColumn(); DC.ColumnName = "Depth"; DC.DataType = System.Type.GetType("System.Int32"); Tables.Columns.Add(DC); } } }