bool CMy1Dlg::preorder(HTREEITEM t)
{
if(t)
{
UpdateData(true)
m_show+=c_tree.GetItemText(t)
m_show+=_T(",")
UpdateData(false)
if(preorder(c_tree.GetNextSiblingItem(t)))
{
if(preorder(c_tree.GetChildItem(t)))
{
return true
}
}
return false
}
else
{
return true
}
}
void CMy1Dlg::OnBnClickedButton2()
{
// TODO: 在此添加控件通知处理程序代码
m_show=_T("")
UpdateData(false)
preorder(c_tree.GetRootItem())
}
其中c_tree是树控件的变量,m_show是一个编辑框的CString变量,用来输出遍历的结果,preorder这个函数放在对话框类的中
这里的OnBnClickedButton2这个按钮事件是用来触发遍历的
如果需要源程序的话留个邮箱发给你
从你的数据表中读出父子节点表到DataTable中,然后按上下级关系一个个添加就可以。添加父子节点只需另写一个功能很数据表中加就行,比较简单。给你一个示例,大概是这样子。
private void ShowTree()
{
this.SystemTree.Nodes.Clear()
TreeNode rootNode = new TreeNode()
rootNode.Text = "XXXXXXXX"
rootNode.Value = ""
int status = 0
rootNode.NavigateUrl = "javascript:SelectNode('root',' '," + Convert.ToString(nodeid++) + "," + status + ")"
this.SystemTree.Nodes.Add(rootNode)
SubsystemManagement subSystem = new SubsystemManagement()
DataTable dt = new DataTable()
dt = subSystem.GetAllsubsystemInfo() //显示子系统名称
foreach (DataRow dr in dt.Rows)
{
TreeNode subNode = new TreeNode()
string subSysName = dr["CFunName"].ToString()
subNode.Text = subSysName
string subSysId = dr["CFunctionID"].ToString()
subNode.Value = subSysId
status = 1
subNode.NavigateUrl = "javascript:SelectNode('subsys','" + subSysId + "'," + Convert.ToString((nodeid++)) + "," + status + ")"
rootNode.ChildNodes.Add(subNode)
rootNode.Expanded = true
DataTable sdt = new DataTable()
sdt = subSystem.GetModulesBySubSysId(subSysId)//显示模块名称
foreach (DataRow sdr in sdt.Rows)
{
TreeNode sNode = new TreeNode()
string moduleName = sdr["CFunName"].ToString()
sNode.Text = moduleName
string moduleId = sdr["CFunctionId"].ToString()
sNode.Value = moduleId
status = 1
sNode.NavigateUrl = "javascript:SelectNode('module','" + moduleId + "'," + Convert.ToString((nodeid++)) + "," + status + ")"
subNode.ChildNodes.Add(sNode)
subNode.Expanded = true
DataTable fdt = new DataTable()
fdt = subSystem.GetFunctionsByModuleId(moduleId)//显示功能名称
foreach (DataRow fdr in fdt.Rows)
{
TreeNode fNode = new TreeNode()
fNode.Text = fdr["CFunName"].ToString()
fNode.Value = fdr["CFunctionId"].ToString()
status = 2
fNode.NavigateUrl = "javascript:SelectNode('function','" + fdr["CFunctionId"].ToString() + "'," + Convert.ToString((nodeid++)) + "," + status + ")"
sNode.ChildNodes.Add(fNode)
sNode.Expanded = false
}
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)