2层for循环生成 TreeView
C# TreeView 利用2层for循环生成,代码如下:
//生成树 treeView1.Nodes.Clear(); //封装了数据库查询方法 MyDS_Grid = MyDataClass.getDataSet("SELECT * FROM table;", "tablename"); for (int i = 0; i < MyDS_Grid.Tables[0].Rows.Count; i++) { //将一级菜单项的名称添加到TreeView组件的根节点中,并设置当前节点的子节点newNode1 TreeNode newNode1 = treeView1.Nodes.Add(MyDS_Grid.Tables[0].Rows[i][1].ToString()); DataSet Area_Grid = MyDataClass.getDataSet("SELECT * FROM table where ID = '"+ MyDS_Grid.Tables[0].Rows[i][0]+"'", "tablename"); for (int j = 0; j < Area_Grid.Tables[0].Rows.Count; j++) { newNode1.Nodes.Add(Area_Grid.Tables[0].Rows[j][2].ToString()); } }
效果图如下:
本文来自博客园,作者:瘦腿先生Stephen,转载请注明原文链接:https://www.cnblogs.com/Stephen-leg/p/18041958