日期:2014-05-18 浏览次数:20854 次
TreeView+ImageList控件 TreeView的ImageList属性指定一个ImageList控件,Imagelist控件中放你存的图片 private void bindTreeView1() { string sql = "select * from dm_category"; DataTable dt = db.ExecuteDataTable(sql, CommandType.Text, null); DataRow[] dr = dt.Select("parent_id is null"); for (int i = 0; i < dr.Length; i++) { TreeNode tn = new TreeNode(); tn.Text = dr[i]["category_name"].ToString(); tn.Tag = dr[i]["id"].ToString(); if (dr[i]["parent_id"].ToString() == "") { FillTree(tn, dt); tn.ImageIndex = 0; } treeView1.Nodes.Add(tn); } } private void FillTree(TreeNode node, DataTable dt) { DataRow[] drr = dt.Select("parent_id='" + node.Tag.ToString() + "'"); if (drr.Length > 0) { for (int i = 0; i < drr.Length; i++) { TreeNode tnn = new TreeNode(); tnn.Text = drr[i]["category_name"].ToString(); tnn.Tag = drr[i]["id"].ToString(); if (drr[i]["parent_id"].ToString() == node.Tag.ToString()) { FillTree(tnn, dt); tnn.ImageIndex = 1; } node.Nodes.Add(tnn); } } }