TreeView和ImageList
TreeView和ImageList
初学者因为所知道的知识有限所以有很多地方做的可能不合常理,如果大家有什么建议或想法,大家尽管说。
数据库建表(这里用的是Oracle)
数据库操作类代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 using System.Data; 5 using System.Data.OracleClient; 6 7 namespace test 8 { 9 class Conn 10 { 11 /// <summary> 12 /// 创建数据库连接 13 /// </summary> 14 private static OracleConnection connection; 15 /// <summary> 16 /// 连接字符串 17 /// </summary> 18 public static OracleConnection Conne 19 { 20 get 21 { 22 string connectionString = "Data Source = 数据库地址;User ID=用户名;password=密码"; 23 if (connection == null) 24 { 25 connection = new OracleConnection(connectionString); 26 connection.Open(); 27 } 28 else if (connection.State == ConnectionState.Closed) 29 { 30 connection.Open(); 31 } 32 else if (connection.State == ConnectionState.Broken) 33 { 34 connection.Close(); 35 connection.Open(); 36 } 37 return connection; 38 } 39 } 40 41 /// <summary> 42 /// 查询TreeView根节点 43 /// </summary> 44 /// <returns></returns> 45 public static DataTable Select() 46 { 47 string sql = "select sysid,sysparentid,sysname,syslevel from t_sysinfo"; 48 OracleDataAdapter adapter = new OracleDataAdapter(sql, Conne); 49 DataSet ds = new DataSet(); 50 adapter.Fill(ds); 51 return ds.Tables[0]; 52 } 53 } 54 }
From窗体:
窗体说明:TreeView name为treeView1,ImageList为imageList1;
ImageList name为imageList1;
窗体代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Text; 7 using System.Windows.Forms; 8 using System.Data.OracleClient; 9 10 namespace test 11 { 12 public partial class Form1 : Form 13 { 14 private DataTable dt; 15 public Form1() 16 { 17 InitializeComponent(); 18 dt = Conn.Select(); 19 } 20 21 private void Form1_Load(object sender, EventArgs e) 22 { 23 FillTreeview(); 24 } 25 26 /// <summary> 27 /// 生成树信息并添加根节点 28 /// </summary> 29 public void FillTreeview() 30 { 31 this.treeView1.Nodes.Clear(); 32 this.treeView1.ImageIndex = 0; 33 this.treeView1.ImageList = this.imageList1; 34 DataRow[] dr = dt.Select("syslevel = '0'"); 35 for (int i = 0; i < dr.Length; i++) 36 { 37 string parentid = dr[i]["sysid"].ToString(); 38 string nodename = dr[i]["sysname"].ToString(); 39 TreeNode rootnode = new TreeNode(); 40 rootnode.Text = nodename; 41 rootnode.ImageIndex = 3; 42 rootnode.SelectedImageIndex = 3; 43 treeView1.Nodes.Add(rootnode); 44 TreenView_ClidNode(rootnode, parentid); 45 } 46 } 47 48 /// <summary> 49 /// 添加子节点 50 /// </summary> 51 /// <param name="node">根节点</param> 52 /// <param name="id">根节点id</param> 53 public void TreenView_ClidNode(TreeNode node, string id) 54 { 55 DataRow[] dr = dt.Select("sysparentid = '" + id + "'"); 56 for (int i = 0; i < dr.Length; i++) 57 { 58 string parentid = dr[i]["sysid"].ToString(); 59 string nodename = dr[i]["sysname"].ToString(); 60 TreeNode clidnode = new TreeNode(); 61 clidnode.Text = nodename; 62 clidnode.SelectedImageIndex = 1; 63 clidnode.ImageIndex = 1; 64 node.Nodes.Add(clidnode); 65 TreenView_ClidNode(clidnode, parentid); 66 if (clidnode.Nodes.Count == 0) 67 { 68 clidnode.ImageIndex = 0; 69 clidnode.SelectedImageIndex = 0; 70 } 71 } 72 } 73 74 /// <summary> 75 /// 展开节点触发的事件 76 /// </summary> 77 /// <param name="sender"></param> 78 /// <param name="e"></param> 79 private void treeView1_AfterExpand(object sender, TreeViewEventArgs e) 80 { 81 TreeView tv = treeView1; 82 TreeNode tn = e.Node; //获取点击加号时所对应的节点 83 if (tn.Level == 0) 84 { 85 this.treeView1.TopNode.ImageIndex = 3; 86 this.treeView1.TopNode.SelectedImageIndex = 3; 87 } 88 else if (tn.Nodes.Count == 0) 89 { 90 tn.ImageIndex = 0; 91 tn.SelectedImageIndex = 0; 92 } 93 else 94 { 95 tn.ImageIndex = 2; 96 tn.SelectedImageIndex = 2; 97 } 98 } 99 100 101 /// <summary> 102 /// 闭合节点所触发的事件 103 /// </summary> 104 /// <param name="sender"></param> 105 /// <param name="e"></param> 106 private void treeView1_AfterCollapse(object sender, TreeViewEventArgs e) 107 { 108 TreeView tv = treeView1; 109 TreeNode tn = e.Node; 110 if (tn.Level == 0) 111 { 112 this.treeView1.TopNode.ImageIndex = 3; 113 this.treeView1.TopNode.SelectedImageIndex = 3; 114 } 115 else if (tv.SelectedNode.Nodes.Count == 0) 116 { 117 tn.ImageIndex = 0; 118 tn.SelectedImageIndex = 0; 119 } 120 else 121 { 122 tn.ImageIndex = 1; 123 tn.SelectedImageIndex = 1; 124 } 125 } 126 } 127 }