日期:2014-05-17 浏览次数:20458 次
void BindData() { DataSet ds = new XJBLL.xjBookType().GetList(""); TreeView1.Nodes.Clear(); TreeNode node = new TreeNode(); node.Value = ds.Tables[0].Rows[0]["ID"].ToString(); node.Text = ds.Tables[0].Rows[0]["TypeName"].ToString(); node.Expand(); TreeView1.Nodes.Add(node); BindChild(node, node.Value); } void BindChild(TreeNode node, string ParentID) { DataSet ds = new XJBLL.xjBookType().GetList(" and ParentID='" + ParentID + "' "); for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { TreeNode childnode = new TreeNode(); childnode.Value = ds.Tables[0].Rows[i]["ID"].ToString(); childnode.Text = ds.Tables[0].Rows[i]["TypeName"].ToString(); node.ChildNodes.Add(childnode); BindChild(childnode, childnode.Value); } }
public System.Data.DataTable dt; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { dt = new System.Data.DataTable(); dt.Columns.Add(new System.Data.DataColumn("ID", typeof(System.String))); dt.Columns.Add(new System.Data.DataColumn("TypeName", typeof(System.String))); dt.Columns.Add(new System.Data.DataColumn("ParentID", typeof(System.String))); dt.Rows.Add("01", "根", ""); dt.Rows.Add("0101", "子类1", "01"); dt.Rows.Add("0102", "子类2", "01"); dt.Rows.Add("0103", "子类3", "01"); dt.Rows.Add("010101", "子类1_1", "0101"); dt.Rows.Add("010102", "子类1_2", "0101"); dt.Rows.Add("010103", "子类1_3", "0101"); //以上数据可以从数据库查询 BindData(); } } void BindData() { TreeView1.Nodes.Clear(); TreeNode node = new TreeNode(); node.Value = dt.Rows[0]["ID"].ToString(); node.Text = dt.Rows[0]["TypeName"].ToString(); node.Expand(); TreeView1.Nodes.Add(node); BindChild(node, node.Value); } void BindChild(TreeNode node, string ParentID) { System.Data.DataRow[] dr = dt.Select("ParentID='" + ParentID + "'","ID"); for (int i = 0; i < dr.Length; i++) { TreeNode childnode = new TreeNode(); childnode.Value = dr[i]["ID"].ToString(); childnode.Text = dr[i]["TypeName"].ToString(); node.ChildNodes.Add(childnode); BindChild(childnode, childnode.Value); } }