C# List构造TreeView
今天根据任务做了从数据库中查询得到两个List,然后又通过List构造TreeView;
1 数据库中有两张表:
权限类型表:类型ID Function_TypeID <PK>
类型编码 Function_TypeCode
类型名称 Function_TypeName
类型的父ID Function_TypeParentID
权限信息表:权限ID Function_ID
权限编码:Function_Code
权限名称:Function_Name
权限类型ID: Function_TypeID
权限描述:Function_Describe
2要根据权限类型表中获得权限类型List构造权限类型树,在权限类型树的基础上再根据从权限信息表中获得的权限List构造权限节点:
源代码
namespace MES.Business.UserPermission
{
public class SystemFunctionManager
{
public void FunctionTreeInit(TreeView toTreeView)
{
List<FunctionType> lstFunctionType = new FunctionTypeManager().GetAllFunctionType();
List<SystemFunction> lstFunction = GetAllSystemFunction();
FunctionTypeTreeInit(ref lstFunctionType, toTreeView);//构建权限类型树
FunctionNodeInit(ref lstFunction, toTreeView);//在权限类型树的基础上构建权限对象
}
/// <summary>
/// 实现对权限分类TreeView的初始化
/// </summary>
/// <param name="lstFunctionType">获得的权限分类List</param>
/// <param name="toTreeView">指定的一个目标TreeView</param>
/// 作者;阮班波
/// 日期;2009-03-16
public void FunctionTypeTreeInit(ref List<FunctionType> lstFunctionType ,TreeView toTreeView)
{
toTreeView.Nodes.Clear();
TreeNode tmpNode = new TreeNode();
foreach(FunctionType objFunctionType in lstFunctionType)
{
if(objFunctionType.Function_TypeParentID == null)
{
TreeNode rootNode = new TreeNode ();
rootNode.Name =objFunctionType.Function_TypeID.ToString();
rootNode.Text = objFunctionType.Function_TypeName;
rootNode.Tag = objFunctionType.Function_TypeCode;
toTreeView.Nodes.Add(rootNode);
rootNode.Expand();
}
else
{
tmpNode = null;
for(int i = 0;i<toTreeView.Nodes.Count;i++)
{
TreeNode ttNode = new TreeNode();
ttNode = FindNode(toTreeView.Nodes[i], objFunctionType.Function_TypeParentID.ToString().Trim());
if(ttNode!=null) tmpNode = ttNode;
}
if(tmpNode!=null)
{
TreeNode subNode = new TreeNode();
subNode.Text = objFunctionType.Function_TypeName;
subNode.Name = objFunctionType.Function_TypeID.ToString();
subNode.Tag = objFunctionType.Function_TypeCode;
tmpNode.Nodes.Add(subNode);
subNode.Expand();
}
}
}
}
/// <summary>
/// 在指定的TreeView上增加权限节点
/// </summary>
/// <param name="lstFunction">权限对象列表</param>
/// <param name="toTreeView">指定的一个目标TreeView</param>
private void FunctionNodeInit(ref List<SystemFunction> lstFunction,TreeView toTreeView)
{
TreeNode tmpNode = new TreeNode();
foreach(SystemFunction objSystemFunction in lstFunction)
{
tmpNode = null;
for(int i = 0;i<toTreeView.Nodes.Count;i++)
{
TreeNode ttNode = new TreeNode();
ttNode = FindNode(toTreeView.Nodes[i],objSystemFunction.Function_TypeID.ToString().Trim());
if (ttNode != null) tmpNode = ttNode;
}
if (tmpNode != null)
{
TreeNode subNode = new TreeNode();
subNode.Name = objSystemFunction.FunctionCode;
subNode.Text = objSystemFunction.FunctionName;
tmpNode.Nodes.Add(subNode);
subNode.Expand();
}
}
}
/// <summary>
/// 递归查找父节点
/// </summary>
/// <param name="tnParent">指定一个根节点,然后遍历它</param>
/// <param name="strValue">所要查找的节点的Name</param>
/// 作者:阮班波
/// 日期:2009-03-16
private TreeNode FindNode(TreeNode tnParent, string strValue)
{
if (tnParent == null) return null;
if (tnParent.Name == strValue) return tnParent;
TreeNode tnRet = null;
foreach (TreeNode tn in tnParent.Nodes)
{
tnRet = FindNode(tn, strValue);
if (tnRet != null) break;
}
return tnRet;
}
}
}
namespace MES.Business.UserPermission
{
public class SystemFunctionManager
{
public void FunctionTreeInit(TreeView toTreeView)
{
List<FunctionType> lstFunctionType = new FunctionTypeManager().GetAllFunctionType();
List<SystemFunction> lstFunction = GetAllSystemFunction();
FunctionTypeTreeInit(ref lstFunctionType, toTreeView);//构建权限类型树
FunctionNodeInit(ref lstFunction, toTreeView);//在权限类型树的基础上构建权限对象
}
/// <summary>
/// 实现对权限分类TreeView的初始化
/// </summary>
/// <param name="lstFunctionType">获得的权限分类List</param>
/// <param name="toTreeView">指定的一个目标TreeView</param>
/// 作者;阮班波
/// 日期;2009-03-16
public void FunctionTypeTreeInit(ref List<FunctionType> lstFunctionType ,TreeView toTreeView)
{
toTreeView.Nodes.Clear();
TreeNode tmpNode = new TreeNode();
foreach(FunctionType objFunctionType in lstFunctionType)
{
if(objFunctionType.Function_TypeParentID == null)
{
TreeNode rootNode = new TreeNode ();
rootNode.Name =objFunctionType.Function_TypeID.ToString();
rootNode.Text = objFunctionType.Function_TypeName;
rootNode.Tag = objFunctionType.Function_TypeCode;
toTreeView.Nodes.Add(rootNode);
rootNode.Expand();
}
else
{
tmpNode = null;
for(int i = 0;i<toTreeView.Nodes.Count;i++)
{
TreeNode ttNode = new TreeNode();
ttNode = FindNode(toTreeView.Nodes[i], objFunctionType.Function_TypeParentID.ToString().Trim());
if(ttNode!=null) tmpNode = ttNode;
}
if(tmpNode!=null)
{
TreeNode subNode = new TreeNode();
subNode.Text = objFunctionType.Function_TypeName;
subNode.Name = objFunctionType.Function_TypeID.ToString();
subNode.Tag = objFunctionType.Function_TypeCode;
tmpNode.Nodes.Add(subNode);
subNode.Expand();
}
}
}
}
/// <summary>
/// 在指定的TreeView上增加权限节点
/// </summary>
/// <param name="lstFunction">权限对象列表</param>
/// <param name="toTreeView">指定的一个目标TreeView</param>
private void FunctionNodeInit(ref List<SystemFunction> lstFunction,TreeView toTreeView)
{
TreeNode tmpNode = new TreeNode();
foreach(SystemFunction objSystemFunction in lstFunction)
{
tmpNode = null;
for(int i = 0;i<toTreeView.Nodes.Count;i++)
{
TreeNode ttNode = new TreeNode();
ttNode = FindNode(toTreeView.Nodes[i],objSystemFunction.Function_TypeID.ToString().Trim());
if (ttNode != null) tmpNode = ttNode;
}
if (tmpNode != null)
{
TreeNode subNode = new TreeNode();
subNode.Name = objSystemFunction.FunctionCode;
subNode.Text = objSystemFunction.FunctionName;
tmpNode.Nodes.Add(subNode);
subNode.Expand();
}
}
}
/// <summary>
/// 递归查找父节点
/// </summary>
/// <param name="tnParent">指定一个根节点,然后遍历它</param>
/// <param name="strValue">所要查找的节点的Name</param>
/// 作者:阮班波
/// 日期:2009-03-16
private TreeNode FindNode(TreeNode tnParent, string strValue)
{
if (tnParent == null) return null;
if (tnParent.Name == strValue) return tnParent;
TreeNode tnRet = null;
foreach (TreeNode tn in tnParent.Nodes)
{
tnRet = FindNode(tn, strValue);
if (tnRet != null) break;
}
return tnRet;
}
}
}