请教高人了
table1
id dept(部门) parent(所属部门ID)
1 海南分公司 0
2 财务部 1
table2
n_id name(姓名) dept_id(所在部门)
1 张三 2树形:
|—海南分公司
|—财务部
|—张三
这个树形结构的语句要怎么实现。。第一次用这个东西。
希望给个例子。。最好能给说说用dataset关联如何关联
谢谢!
table1
id dept(部门) parent(所属部门ID)
1 海南分公司 0
2 财务部 1
table2
n_id name(姓名) dept_id(所在部门)
1 张三 2树形:
|—海南分公司
|—财务部
|—张三
这个树形结构的语句要怎么实现。。第一次用这个东西。
希望给个例子。。最好能给说说用dataset关联如何关联
谢谢!
{
//根据ParentID读取数据,生成Node并且填充ParentNode
。
//递归调用
}
检索table1表,填充dataset对象public void LoadTree(TreeNodeCollection Nds, string parentId)
{
TreeNode tmpNd; //建立一个节点
DataRow[] rows = ds.Tables[0].Select("parent='" + parentId + "'"); foreach (DataRow row in rows)
{
//通过查询得到父节点,依次循环
tmpNd = new TreeNode(); //声明一个新的节点
tmpNd.Name = row["id "].ToString();//节的名字是a.ToString()
tmpNd.Text = row["dept"].ToString();//节点的内容是b.ToString()
Nds.Add(tmpNd);//把节点加入到节点集合中
InitTree(tmpNd.Nodes, tmpNd.Name);//把加入的节点作为新的节点集合再次调用 名字是a.ToString()
}
}
{
//根据ParentID读取数据,生成Node并且填充ParentNode
DataTable table = objDAL.GetData(ParentID);
foreach(Row row in table.Rows)
{
//根据row中的数据构造个Node
Node node = New Node(row["dept"].ToString());
ParentNode.Nodes.Add(node);
//递归调用,读取并构造下级节点
AddChildren(node,row["id"]);
}
}
private TreeNode FindNode( TreeNode tnParent, string strValue )
{
if( tnParent == null ) return null;
if( tnParent.Text == strValue ) return tnParent;
TreeNode tnRet = null;
foreach( TreeNode tn in tnParent.Nodes )
{
tnRet = FindNode( tn, strValue );
if( tnRet != null ) break;
}
return tnRet;
}
第二种,非递归算法,代码如下:
private TreeNode FindNode( TreeNode tnParent, string strValue )
{
if( tnParent == null ) return null;
if( tnParent.Text == strValue ) return tnParent;
else if( tnParent.Nodes.Count == 0 ) return null;
TreeNode tnCurrent, tnCurrentPar;
//Init node
tnCurrentPar = tnParent;
tnCurrent = tnCurrentPar.FirstNode;
while( tnCurrent != null && tnCurrent != tnParent )
{
while( tnCurrent != null )
{
if( tnCurrent.Text == strValue ) return tnCurrent;
else if( tnCurrent.Nodes.Count > 0 )
{
//Go into the deepest node in current sub-path
tnCurrentPar = tnCurrent;
tnCurrent = tnCurrent.FirstNode;
}
else if( tnCurrent != tnCurrentPar.LastNode )
{
//Goto next sible node
tnCurrent = tnCurrent.NextNode;
}
else
break;
}
//Go back to parent node till its has next sible node
while( tnCurrent != tnParent && tnCurrent == tnCurrentPar.LastNode )
{
tnCurrent = tnCurrentPar;
tnCurrentPar = tnCurrentPar.Parent;
}
//Goto next sible node if( tnCurrent != tnParent )
tnCurrent = tnCurrent.NextNode;
}
return null;
}
程序调用,如下:
TreeNode tnRet = null;
foreach( TreeNode tn in yourTreeView.Nodes )
{
tnRet = FindNode( tn, yourValue );
if( tnRet != null ) break;
}
2.绑定 根节点的所有部门节点
3.判断 树节点的部门ID 在人员表中是否存在
yes 在此节点下加载 人员节点;
no 就不用加了;
private void BindTreeRoot()
{
////初始化根
CDepartBLL bll = new CDepartBLL();
List<CDepartDAL> alist = bll.GetDepartTreeNode("DeptId = 1");
List<CDepartDAL> rlist = bll.GetDepartTreeNode("DeptId <> 1");
if (rlist.Count != 0)
{
foreach (CDepartDAL dept in alist)
{
TreeNode rootnode = new TreeNode();
rootnode.Text = dept.DeptName;
rootnode.Name = dept.DeptId.ToString();
rootnode.Tag = alist;
TreeNodeAdd(rootnode, dept.DeptId);
this.treeView1.Nodes.Add(rootnode);
}
}
} private void TreeNodeAdd(TreeNode node,int deptid)
{
CDepartBLL bll = new CDepartBLL();
List<CDepartDAL> rlist = bll.GetDepartTreeNode("DeptId <> 1 AND DeptRalation= " + deptid + "");
foreach (CDepartDAL dept in rlist)
{
TreeNode childnode = new TreeNode();
childnode.Text = dept.DeptName;
childnode.Name = dept.DeptId.ToString();
childnode.Tag = rlist;
if (rlist.Count != 0)
{
TreeNodeAdd(childnode, dept.DeptId);
}
node.Nodes.Add(childnode);
} }
这个CDepartBLL是什么?