今天根據任務做了從資料庫中查詢得到兩個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;
}
}
}