想要形成這種效果的話,,首先在數(shù)據(jù)里面建一張表
create table treedata (id number, context varchar2(50), parentid number);
id:當(dāng)前的id,,context當(dāng)前節(jié)點(diǎn)的值文本,,parentid代表當(dāng)前節(jié)點(diǎn)的父節(jié)點(diǎn)id
insert into treedata values(1, '所有部門', 0);
insert into treedata values(2, '人事部', 1);
insert into treedata values(3, '財(cái)務(wù)部', 1);
insert into treedata values(4, '生產(chǎn)部', 1);
insert into treedata values(5, '車間一', 4);
insert into treedata values(6, '車間二', 4);
insert into treedata values(7, '車間三', 4);
insert into treedata values(8, '商務(wù)部', 1);
再用遞歸根據(jù)數(shù)據(jù)庫(kù)查詢的值動(dòng)態(tài)綁定treeView
- public void AddTree(int ParentID, TreeNode pNode)
- {
- string sql = "select id, context, parentid from treedata";
- DataSet ds = OracleHelper.ExecuteDataset(OracleHelper.Con, CommandType.Text, sql, null);
- TreeNode tn1 = new TreeNode();
- DataView dvTree = new DataView(ds.Tables[0]);
- //過濾ParentID,得到當(dāng)前的所有子節(jié)點(diǎn)
- dvTree.RowFilter = "[PARENTID] = " + ParentID;
- foreach (DataRowView Row in dvTree)
- {
- if (pNode == null)
- { //'?添加根節(jié)點(diǎn)
-
- tn1.Text = Row["ConText"].ToString();
- treeView1.Nodes.Add(tn1);
- tn1.ExpandAll();
- AddTree(Int32.Parse(Row["ID"].ToString()), tn1); //再次遞歸
- }
- else
- { //添加當(dāng)前節(jié)點(diǎn)的子節(jié)點(diǎn)
- TreeNode tn2 = new TreeNode();
- tn2.Text = Row["ConText"].ToString();
- pNode.Nodes.Add(tn2);
- tn1.ExpandAll();
- AddTree(Int32.Parse(Row["ID"].ToString()), tn2); //再次遞歸
- }
- }
- treeView1.ExpandAll();
- }
應(yīng)用:
- private void 人員資料管理_Load(object sender, EventArgs e)
- {
- AddTree(0, (TreeNode)null);
- treeView1.ExpandAll();//默認(rèn)展開所有節(jié)點(diǎn)
- this.toolStripComboBox1.SelectedIndex = 0;
- }
就可以了。,。,。。
|