treeview递归获取部门(树状结构)
private void yuangongjiansuo_Load(object sender, EventArgs e)
{
CreateTable();
CreateTree();
}
private DataTable dtTree = null;
private DataView dv = null;
private void CreateTable()
{
OracleConnection conn = new OracleConnection("Data Source=orcl;User ID=TODAI;Password=TODAI");
conn.Open();
string sss = "Select DEPTID,DEPTNAME,PARENT_DEPT_ID from HR_DEPARTMENT";
OracleDataAdapter kkk = new OracleDataAdapter(sss, conn);
DataSet ds = new DataSet();
kkk.Fill(ds);
dtTree = ds.Tables[0];
}
private void CreateTree()
{
dv = dtTree.DefaultView;
dv.Sort = "PARENT_DEPT_ID ASC";
DataRowView[] arrDRV = dv.FindRows(0);
if (arrDRV.Length == 0) return;
TreeNode tnNew = null;
foreach (DataRowView drv in arrDRV)
{
tnNew = treeView1.Nodes.Add(drv.Row["DEPTNAME"].ToString());
tnNew.Tag = drv.Row["DEPTID"].ToString();
CreateTreeNode(tnNew);
}
this.treeView1.ExpandAll();
}
private void CreateTreeNode(TreeNode tnParent)
{
DataRowView[] arrDRV = dv.FindRows(tnParent.Tag);
if (arrDRV.Length == 0) return;
TreeNode tnNew = null;
foreach (DataRowView drv in arrDRV)
{
tnNew = tnParent.Nodes.Add(drv.Row["DEPTNAME"].ToString());
tnNew.Tag = drv.Row["DEPTID"].ToString();
CreateTreeNode(tnNew);
}
}