其实这类算法挺多的,还有就是听说用微软的TreeView对于节点太多就不太适合了,下边的代码使用HashTable,仅供参考:
Hashtable httree = new Hashtable();
foreach(DataRow myrow in dtregion.Rows)
{
TreeNode newnode = new TreeNode();
newnode.ID = myrow["ID"].ToString();
newnode.Text = myrow["Name"].ToString();
newnode.NodeData = myrow["ParentID"].ToString();
httree.Add(newnode.ID,newnode);
}
foreach(DataRow treerow in dtregion.Rows)
{
TreeNode newnode = (TreeNode)httree[treerow["ID"].ToString()];
TreeNode parentnode = (TreeNode)httree[newnode.NodeData];
if(parentnode != null)
{
parentnode.Nodes.Add(newnode);
}
else
{
this.TreeView1.Nodes.Add(newnode);
}
}
Hashtable httree = new Hashtable();
foreach(DataRow myrow in dtregion.Rows)
{
TreeNode newnode = new TreeNode();
newnode.ID = myrow["ID"].ToString();
newnode.Text = myrow["Name"].ToString();
newnode.NodeData = myrow["ParentID"].ToString();
httree.Add(newnode.ID,newnode);
}
foreach(DataRow treerow in dtregion.Rows)
{
TreeNode newnode = (TreeNode)httree[treerow["ID"].ToString()];
TreeNode parentnode = (TreeNode)httree[newnode.NodeData];
if(parentnode != null)
{
parentnode.Nodes.Add(newnode);
}
else
{
this.TreeView1.Nodes.Add(newnode);
}
}