/// <summary> /// 生成树形结构 /// </summary> public void GetTreeNode() { //SqlHelper.GetSqlDataReader是封装的查询数据库语句,可根据自己需求封装 //假设获取所有一级节点 List<Products> products = SqlHelper.GetSqlDataReader(sql); for (int i = 0; i < products.Count; i++) { TreeNode tree = new TreeNode() { Name = products[i].ProductId.ToString(), Text = products[i].ProductName }; //假设根据一级节点CategoryId获取二级节点 List<SalesPerson> salesPerson = SqlHelper.GetSqlDataReader(products[i].CategoryId.ToString()); for (int j = 0; j < salesPerson.Count; j++) { tree.Nodes.Add(new TreeNode() { Name= salesPerson[j].SalesPersonId.ToString(), Text = salesPerson[j].SPName, ForeColor=Color.Blue }); //获取一级节点ProductId获取三级节点 List<SMMembers> sMMembers = SqlHelper.GetSqlDataReader(products[i].ProductId.ToString()); for(int k = 0; k < sMMembers.Count; k++) { tree.Nodes[j].Nodes.Add(new TreeNode() { Name = sMMembers[k].MemberId.ToString(), Text= sMMembers[k].MemberName, Tag = sMMembers[k].MemberStatus, ForeColor=Color.Red //设置颜色 }); }; } this.tvTreeNode.Nodes.Add(tree); //将节点数据添加到treeView控件中 } this.tvTreeNode.ExpandAll(); //展开所有节点 }
标签:sMMembers,SqlHelper,c#,创建,树形,products,ToString,节点 From: https://www.cnblogs.com/tlfe/p/18280056