日期:2014-05-16 浏览次数:20706 次
两种方式 ,根据数据库表的创建不同而不同。
?
一个是根节点是一张表,子节点在另一张表,是主外键的关系 ,那么是:
??? //treeview 1 循环添加父节点
??????? public void bbb()
??? {
??????? DataTable dt = DataAccess.Sys.Get_Zb_DeptCa1("");
??????? foreach (DataRow row in dt.Rows)
??????? {
??????????? TreeNode node = CreateNode(Convert.ToInt32(row["DeptCa_Code"]), Convert.ToString(row["DeptCa_Name"]));
//调用子节点
??????????? CCreateChild(Convert.ToInt32(row["DeptCa_Code"]),node);
//吧节点添加到treeview中
??????????? this.TreeView1.Nodes.Add(node);
??????? }
??? }
??????? //公用的节点
??????? public TreeNode CreateNode(int id, string name)
??????? {
??????????? TreeNode node = new TreeNode();
??????????? node.Text = name;
????????? //? node.ImageUrl = imgs;
??????????? return node;
??????? }
??????? //treeview 1 循环添加子节点
??????? public void CCreateChild(int parentid, TreeNode node)
??? {
??????? DataTable dt = DataAccess.Sys.Get_Zb_DeptByCaid(parentid);
??????? foreach (DataRow row in dt.Rows)
??????? {
??????????? TreeNode nodes = CreateNode(Convert.ToInt32(row["Dept_Code"]), Convert.ToString(row["Dept_Name"]));
???????? // 这句话可以不要?? nodes.NavigateUrl = "ShowSecondOne.aspx?ltid=" + Convert.ToInt32(row["id"]);
??????????? node.ChildNodes.Add(nodes);
??????? }
?
要是所有节点在同一张表内,则是:
数据结构如下: CREATE TABLE [dbo].[S_Menu] ( ?[NodeId] [char] (6) COLLATE Chinese_PRC_CI_AS NULL , ?[ParentId] [char] (6) COLLATE Chinese_PRC_CI_AS NULL , ?[NodeName] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL , ?[Address] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL , ?[Icon] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ) ON [PRIMARY] GO
?
?//建树的基本思路是:从根节点开始递归调用显示子树
??????? ?private void InitTree(TreeNodeCollection Nds,string parentId)
??{
???DataView dv=new DataView();
???TreeNode tmpNd;
???string intId;
???dv.Table=ds.Tables["tree"];
???dv.RowFilter="ParentId="" + parentId + """ ;
???foreach(DataRowView drv