用树来实现,速度比较慢: using System; using System.Collections; using System.Data; using System.Data.SqlClient;public class Partment { public static void Main() { Partment p = new Partment("server=localhost;user id=sa;password=;database=master;"); Console.WriteLine(p.root.Id+":"+p.root.Name); print(p.root, 0); } static void print(Tree t, int level) { for (int i = 0; i < t.ChildCount(); i++) { Tree ch = t.Child(i); for (int j = 0; j < level + 1; j++) { Console.Write(" "); } Console.WriteLine(ch.Id+":"+ch.Name); if (ch.ChildCount() > 0) { print(ch, level + 1); } } } Tree root; DataTable table; public Partment(String conn) { Init(conn); } public void Init(String conn) { string sql = "select * from department order by ID, MotherID"; SqlDataAdapter sda = new SqlDataAdapter(sql, conn); table = new DataTable("department"); try { sda.Fill(table); } catch(Exception err) { //±êÖ¾ return; } AddData(); } private void AddData() { int parent = 1; Tree t = null; foreach(DataRow dr in this.table.Rows) { int pid = Convert.ToInt32(dr["MotherID"]); Tree tn = new Tree(); tn.Name = dr["Name"].ToString(); tn.Id = Convert.ToInt32(dr["ID"]); if (root == null) { root = tn; t = root; } else if (parent == pid) { t.AddChild(tn); } else { parent = pid; GetParent(t, pid).AddChild(tn); } } } private static Tree GetParent(Tree par, int parent) { if (par.Id == parent) { return par; } for (int i = 0; i < par.ChildCount(); i++) { Tree t = par.Child(i); if (parent == t.Id) { return t; } }
for (int i = 0; i < par.ChildCount(); i++) { Tree t = par.Child(i); if (t.ChildCount() > 0) { t = GetParent(t, parent); if (t != null) { return t; } } } return null; } public class Tree { public String Name; public int Id; ArrayList child; public Tree() { } public void AddChild(Tree ch) { if (child == null) { child = new ArrayList(); } child.Add(ch); } public int ChildCount() { return child == null ? 0 : child.Count; }
public Tree Child(int index) { return child == null ? null : (Tree) child[index]; } } }
用数据链的概念更好,速度更快。 只需讲AddData方法改一下即可: private void AddData() { ArrayList list = new ArrayList(); foreach(DataRow dr in this.table.Rows) { int pid = Convert.ToInt32(dr["MotherID"]); Tree tn = new Tree(); tn.Name = dr["Name"].ToString(); tn.Id = Convert.ToInt32(dr["ID"]); list.Add(tn); if (root == null) { root = tn; } else { ((Tree) list[pid - 1]).AddChild(tn); } } }
如果是oracle和db2:一條sql就可以出來,如果是sql server則可以寫存儲過程。
部门编号(ID) 部门名称等(Name) 所属上级部门编号(MotherID)字段分别为
departmentID,departMentName ,parentIDprivate DataTable createDepartMent()
{
string sql = "select * from department";
SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
DataTable dt = new DataTable("department");
dt.Clear();
try
{
sda.Fill(dt);
return dt;
}
catch(Exception err)
{
return null;
} } private void Form1_Load(object sender, System.EventArgs e)
{
this.dtDept = this.createDepartMent();
if(dtDept == null)
{
MessageBox.Show("Read Data Error");
return;
}
this.addTreeNode();
} private void addTreeNode()
{
foreach(DataRow dr in this.dtDept.Rows)
{
if(dr["parentId"].ToString()=="0")
{
TreeNode tn = new TreeNode();
tn.Text=dr["departmentName"].ToString();
string strTag = dr["departmentID"].ToString();
tn.Tag=dr["departmentID"].ToString();
this.TreeDpt.Nodes.Add(tn);
this.addNodes(tn); }
}
}
private void addNodes(TreeNode parentn)
{
foreach(DataRow dr in this.dtDept.Rows)
{
string parentid = dr["parentId"].ToString();
string strID = parentn.Tag.ToString();
if(parentid==strID)
{
TreeNode tn = new TreeNode();
tn.Text=dr["departmentName"].ToString();
tn.Tag=dr["departmentID"].ToString();
parentn.Nodes.Add(tn);
this.addNodes(tn);
}
} }
第二个循环插入该层的每个子结点。
using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;public class Partment {
public static void Main() {
Partment p = new Partment("server=localhost;user id=sa;password=;database=master;");
Console.WriteLine(p.root.Id+":"+p.root.Name);
print(p.root, 0);
}
static void print(Tree t, int level) {
for (int i = 0; i < t.ChildCount(); i++) {
Tree ch = t.Child(i);
for (int j = 0; j < level + 1; j++) {
Console.Write(" ");
}
Console.WriteLine(ch.Id+":"+ch.Name);
if (ch.ChildCount() > 0) {
print(ch, level + 1);
}
}
}
Tree root;
DataTable table;
public Partment(String conn) {
Init(conn);
} public void Init(String conn)
{
string sql = "select * from department order by ID, MotherID";
SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
table = new DataTable("department");
try
{
sda.Fill(table);
}
catch(Exception err)
{
//±êÖ¾
return;
}
AddData();
} private void AddData() {
int parent = 1;
Tree t = null;
foreach(DataRow dr in this.table.Rows)
{
int pid = Convert.ToInt32(dr["MotherID"]);
Tree tn = new Tree();
tn.Name = dr["Name"].ToString();
tn.Id = Convert.ToInt32(dr["ID"]);
if (root == null) {
root = tn;
t = root;
} else if (parent == pid) {
t.AddChild(tn);
} else {
parent = pid;
GetParent(t, pid).AddChild(tn);
}
}
} private static Tree GetParent(Tree par, int parent) {
if (par.Id == parent) {
return par;
}
for (int i = 0; i < par.ChildCount(); i++) {
Tree t = par.Child(i);
if (parent == t.Id) {
return t;
}
}
for (int i = 0; i < par.ChildCount(); i++) {
Tree t = par.Child(i);
if (t.ChildCount() > 0) {
t = GetParent(t, parent);
if (t != null) {
return t;
}
}
}
return null;
} public class Tree {
public String Name;
public int Id;
ArrayList child;
public Tree() {
} public void AddChild(Tree ch) {
if (child == null) {
child = new ArrayList();
}
child.Add(ch);
} public int ChildCount() {
return child == null ? 0 : child.Count;
}
public Tree Child(int index) {
return child == null ? null : (Tree) child[index];
}
}
}
只需讲AddData方法改一下即可:
private void AddData() {
ArrayList list = new ArrayList();
foreach(DataRow dr in this.table.Rows)
{
int pid = Convert.ToInt32(dr["MotherID"]);
Tree tn = new Tree();
tn.Name = dr["Name"].ToString();
tn.Id = Convert.ToInt32(dr["ID"]);
list.Add(tn);
if (root == null) {
root = tn;
} else {
((Tree) list[pid - 1]).AddChild(tn);
}
}
}