封裝成一個函數,方便直接調用
- //綁定TrreView
- private void InitModuleTree(DataTable dt)
- {
- //清空treeview上所有節(jié)點
- this.tree_Role.Nodes.Clear();
- int[] gen = new int[dt.Rows.Count]; //用于存儲父節(jié)點Tag
- int[] zi = new int[dt.Rows.Count]; //用于存儲子節(jié)點Tag
- for (int i = 0; i < gen.Length; i++)
- {
- string zhi=dt.Rows[i][3].ToString();//獲取節(jié)點Tag值 eg:1-2
- if (zhi.Length>1) //表示是子節(jié)點 eg:1-2
- {
- gen[i] = int.Parse(zhi.Substring(0, zhi.IndexOf('-')));
- zi[i] = int.Parse(zhi.Substring(zhi.IndexOf('-') + 1));
- }
- else //表示是根節(jié)點 eg:2
- {
- //將所有父節(jié)點加到treeview上
- zi[i] =int.Parse(zhi);
- TreeNode nodeParent = new TreeNode();
- nodeParent.Tag = (zi[i]).ToString();
- nodeParent.Text = dt.Rows[i][1].ToString();
- tree_Role.Nodes.Add(nodeParent);
- }
- }
- bindChildNote(dt,gen,zi);
- }
- //綁定子節(jié)點
- private void bindChildNote(DataTable dt, int[] gen, int[] zi)
- {
- for (int i = 0; i < gen.Length; i++)
- {
- if (gen[i] != 0 && zi[i] != 0) //便利所有節(jié)點,,找到所有子節(jié)點
- {
- TreeNode childNode = new TreeNode();
- foreach (TreeNode item in tree_Role.Nodes) //便歷treeview上所有父節(jié)點
- {
- if (item.Tag.ToString() == gen[i].ToString()) //找到當前子節(jié)點的父節(jié)點
- {
- childNode.Tag = zi[i].ToString();
- childNode.Text = dt.Rows[i][1].ToString();
- item.Nodes.Add(childNode);
- }
- }
- }
- }
- tree_Role.ExpandAll(); //展開整棵樹
- }
數據庫結構:
最終結果:
引用代碼:
- namespace TreeeeeeeeeeeView
- {
- public partial class Treeeeeeeeeee : Form
- {
- public Treeeeeeeeeee()
- {
- InitializeComponent();
-
- //從數據庫獲取數據,得到結果為DataTable
- SystemManager.Dao.RolePower dao = new SystemManager.Dao.RolePower();
- DataSet ds = CommonLib.Dao.CompressUtil.DeCompress(dao.getSystemModule());
- DataTable dt = ds.Tables[0];
- InitModuleTree(dt);
- }
-
- //綁定TrreView
- private void InitModuleTree(DataTable dt)
- {
- //清空treeview上所有節(jié)點
- this.tree_Role.Nodes.Clear();
- int[] gen = new int[dt.Rows.Count]; //用于存儲父節(jié)點Tag
- int[] zi = new int[dt.Rows.Count]; //用于存儲子節(jié)點Tag
- for (int i = 0; i < gen.Length; i++)
- {
- string zhi=dt.Rows[i][3].ToString();//獲取節(jié)點Tag值 eg:1-2
- if (zhi.Length>1) //表示是子節(jié)點 eg:1-2
- {
- gen[i] = int.Parse(zhi.Substring(0, zhi.IndexOf('-')));
- zi[i] = int.Parse(zhi.Substring(zhi.IndexOf('-') + 1));
- }
- else //表示是根節(jié)點 eg:2
- {
- //將所有父節(jié)點加到treeview上
- zi[i] =int.Parse(zhi);
- TreeNode nodeParent = new TreeNode();
- nodeParent.Tag = (zi[i]).ToString();
- nodeParent.Text = dt.Rows[i][1].ToString();
- tree_Role.Nodes.Add(nodeParent);
- }
- }
- bindChildNote(dt,gen,zi);
- }
-
- //綁定子節(jié)點
- private void bindChildNote(DataTable dt, int[] gen, int[] zi)
- {
- for (int i = 0; i < gen.Length; i++)
- {
- if (gen[i] != 0 && zi[i] != 0) //便利所有節(jié)點,,找到所有子節(jié)點
- {
- TreeNode childNode = new TreeNode();
- foreach (TreeNode item in tree_Role.Nodes) //便歷treeview上所有父節(jié)點
- {
- if (item.Tag.ToString() == gen[i].ToString()) //找到當前子節(jié)點的父節(jié)點
- {
- childNode.Tag = zi[i].ToString();
- childNode.Text = dt.Rows[i][1].ToString();
- item.Nodes.Add(childNode);
- }
- }
- }
- }
- tree_Role.ExpandAll(); //展開整棵樹
- }
-
- }
- }
|