数据库的数据结构是这样的:
sCompany_no varchar(10) Unchecked
sCompany_name varchar(50) Unchecked
sShortname varchar(30) Checked
sParent_no varchar(10) Checked
sTree varchar(30) Checked
iTreeLevel int Checked
具体数据如下:
构造treeview的c#代码如下:(用递归方法实现)
其中用到了ORM获取数据,不用它当然也可以。
sCompany_no varchar(10) Unchecked
sCompany_name varchar(50) Unchecked
sShortname varchar(30) Checked
sParent_no varchar(10) Checked
sTree varchar(30) Checked
iTreeLevel int Checked
具体数据如下:
000001 | 总公司 | 简称 | NULL | 001 | 1 |
000002 | 公司2 | 简称 | 000001 | 001001 | 2 |
000003 | 公司3 | 简称 | 000001 | 001002 | 2 |
000004 | 公司4 | 简称 | 000001 | 001003 | 2 |
000005 | 公司4 | 简称 | 000001 | 001004 | 2 |
000006 | 公司4下属 | 简称 | 000005 | 001004001 | 3 |
000007 | 公司4下属2 | 简称 | 000005 | 001004002 | 3 |
构造treeview的c#代码如下:(用递归方法实现)
#region 初始化部门树
//--------------------------------------------------------------------------------------------------------
/// <summary>
/// ORM实现对部门树的初始化
/// </summary>
/// <param name="toTreeview">指定一个目标treeview</param>
public static void CompanyTree_Init(TreeView toTreeView)
{
toTreeView.Nodes.Clear();
int iLevel = 1;// tmpCorp.iTreeLevel;
ORtCompany corp = new ORtCompany();
ORDataReader<ORtCompany> reader = corp.DataAccessor.ExecuteReader(CommandType.Text, "select * from tCompany where sStatus = '1'", corp, null);
TreeNode tmpNode = new TreeNode();
foreach (ORtCompany u in reader)//循环每行数据
{
if ((int)u.iTreeLevel == iLevel)
{
TreeNode rootNode = new TreeNode();
rootNode.Text = u.sCompany_no.ToString().Trim() + u.sShortname.ToString().Trim();
rootNode.Value = u.sCompany_no.ToString().Trim();
rootNode.ToolTip = u.sCompany_name;
toTreeView.Nodes.Add(rootNode);
rootNode.Expanded = true;
}
else
{
tmpNode = null;
for (int i = 0; i < toTreeView.Nodes.Count; i++)
{
TreeNode ttNode = new TreeNode();
ttNode = FindNode(toTreeView.Nodes[i], u.sParent_no.ToString().Trim());
if (ttNode != null) tmpNode = ttNode;
}
if (tmpNode != null)
{
TreeNode subNode = new TreeNode();
subNode.Text = u.sCompany_no.ToString().Trim() + u.sShortname.ToString().Trim();
subNode.Value = u.sCompany_no.ToString().Trim();
subNode.ToolTip = u.sCompany_name;
tmpNode.ChildNodes.Add(subNode);
subNode.Expanded = true;
}
}
}
reader.Close();
}
/// <summary>
/// 递归查找父节点
/// </summary>
/// <param name="tnParent">指定一个根节点,然后遍历它</param>
/// <param name="strValue">所要查找的节点的value</param>
public static TreeNode FindNode(TreeNode tnParent, string strValue)
{
if (tnParent == null) return null;
if (tnParent.Value == strValue) return tnParent;
TreeNode tnRet = null;
foreach (TreeNode tn in tnParent.ChildNodes)
{
tnRet = FindNode(tn, strValue);
if (tnRet != null) break;
}
return tnRet;
}
#endregion
//--------------------------------------------------------------------------------------------------------
/// <summary>
/// ORM实现对部门树的初始化
/// </summary>
/// <param name="toTreeview">指定一个目标treeview</param>
public static void CompanyTree_Init(TreeView toTreeView)
{
toTreeView.Nodes.Clear();
int iLevel = 1;// tmpCorp.iTreeLevel;
ORtCompany corp = new ORtCompany();
ORDataReader<ORtCompany> reader = corp.DataAccessor.ExecuteReader(CommandType.Text, "select * from tCompany where sStatus = '1'", corp, null);
TreeNode tmpNode = new TreeNode();
foreach (ORtCompany u in reader)//循环每行数据
{
if ((int)u.iTreeLevel == iLevel)
{
TreeNode rootNode = new TreeNode();
rootNode.Text = u.sCompany_no.ToString().Trim() + u.sShortname.ToString().Trim();
rootNode.Value = u.sCompany_no.ToString().Trim();
rootNode.ToolTip = u.sCompany_name;
toTreeView.Nodes.Add(rootNode);
rootNode.Expanded = true;
}
else
{
tmpNode = null;
for (int i = 0; i < toTreeView.Nodes.Count; i++)
{
TreeNode ttNode = new TreeNode();
ttNode = FindNode(toTreeView.Nodes[i], u.sParent_no.ToString().Trim());
if (ttNode != null) tmpNode = ttNode;
}
if (tmpNode != null)
{
TreeNode subNode = new TreeNode();
subNode.Text = u.sCompany_no.ToString().Trim() + u.sShortname.ToString().Trim();
subNode.Value = u.sCompany_no.ToString().Trim();
subNode.ToolTip = u.sCompany_name;
tmpNode.ChildNodes.Add(subNode);
subNode.Expanded = true;
}
}
}
reader.Close();
}
/// <summary>
/// 递归查找父节点
/// </summary>
/// <param name="tnParent">指定一个根节点,然后遍历它</param>
/// <param name="strValue">所要查找的节点的value</param>
public static TreeNode FindNode(TreeNode tnParent, string strValue)
{
if (tnParent == null) return null;
if (tnParent.Value == strValue) return tnParent;
TreeNode tnRet = null;
foreach (TreeNode tn in tnParent.ChildNodes)
{
tnRet = FindNode(tn, strValue);
if (tnRet != null) break;
}
return tnRet;
}
#endregion
其中用到了ORM获取数据,不用它当然也可以。