c# treeview 类别树 存入数据库并读取出来
1个回答
展开全部
用递归法生成树,给你一个我做的生成公司组织结构方法参考一下吧。
#region 加载所有部门树形
/// <summary>
/// 根据用户ID加载对应的菜单
/// </summary>
/// <param name="userId"></param>
public static void DisplayDemparmnetsTree(TreeView tree)
{
tree.Nodes.Clear();
IList<DepartmentInfo> list = DepartmentInfoManager.GetDepartmentInfosByFatherId("0");
foreach (DepartmentInfo dep in list)
{
TreeNode fathernode = CreatTreeNode(dep.DepartmentName, dep.DepartmentId.ToString(), "", "~/Images/menuclose.gif");
tree.Nodes.Add(fathernode);
CreateChildTree(dep.DepartmentId.ToString(), fathernode);
}
}
private static TreeNode CreatTreeNode(string strText, string strId, string strUrl, string strImg)
{
TreeNode newNode = new TreeNode();
newNode.Text = strText;
newNode.Value = strId;
newNode.NavigateUrl = strUrl;
newNode.ImageUrl = strImg;
return newNode;
}
private static void CreateChildTree(string fatherDepartmentId, TreeNode fatherNode)
{
IList<DepartmentInfo> childlist = DepartmentInfoManager.GetDepartmentInfosByFatherId(fatherDepartmentId);
foreach (DepartmentInfo dep in childlist)
{
TreeNode childNode = CreatTreeNode(dep.DepartmentName, dep.DepartmentId.ToString(), "", "~/Images/menuclose.gif");
fatherNode.ChildNodes.Add(childNode);
CreateChildTree(dep.DepartmentId.ToString(), childNode);
}
}
#endregion
#region 加载所有部门树形
/// <summary>
/// 根据用户ID加载对应的菜单
/// </summary>
/// <param name="userId"></param>
public static void DisplayDemparmnetsTree(TreeView tree)
{
tree.Nodes.Clear();
IList<DepartmentInfo> list = DepartmentInfoManager.GetDepartmentInfosByFatherId("0");
foreach (DepartmentInfo dep in list)
{
TreeNode fathernode = CreatTreeNode(dep.DepartmentName, dep.DepartmentId.ToString(), "", "~/Images/menuclose.gif");
tree.Nodes.Add(fathernode);
CreateChildTree(dep.DepartmentId.ToString(), fathernode);
}
}
private static TreeNode CreatTreeNode(string strText, string strId, string strUrl, string strImg)
{
TreeNode newNode = new TreeNode();
newNode.Text = strText;
newNode.Value = strId;
newNode.NavigateUrl = strUrl;
newNode.ImageUrl = strImg;
return newNode;
}
private static void CreateChildTree(string fatherDepartmentId, TreeNode fatherNode)
{
IList<DepartmentInfo> childlist = DepartmentInfoManager.GetDepartmentInfosByFatherId(fatherDepartmentId);
foreach (DepartmentInfo dep in childlist)
{
TreeNode childNode = CreatTreeNode(dep.DepartmentName, dep.DepartmentId.ToString(), "", "~/Images/menuclose.gif");
fatherNode.ChildNodes.Add(childNode);
CreateChildTree(dep.DepartmentId.ToString(), childNode);
}
}
#endregion
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询