从你的数据表中读出父子节点表到DataTable中,然后按上下级关系一个个添加就可以。添加父子节点只需另写一个功能很数据表中加就行,比较简单。
给你一个示例,大概是这样子。
private void ShowTree()
{
thisSystemTreeNodesClear();
TreeNode rootNode = new TreeNode();
rootNodeText = "XXXXXXXX";
rootNodeValue = "";
int status = 0;
rootNodeNavigateUrl = "javascript:SelectNode('root',' '," + ConvertToString(nodeid++) + "," + status + ")";
thisSystemTreeNodesAdd(rootNode);
SubsystemManagement subSystem = new SubsystemManagement();
DataTable dt = new DataTable();
dt = subSystemGetAllsubsystemInfo(); //显示子系统名称
foreach (DataRow dr in dtRows)
{
TreeNode subNode = new TreeNode();
string subSysName = dr["CFunName"]ToString();
subNodeText = subSysName;
string subSysId = dr["CFunctionID"]ToString();
subNodeValue = subSysId;
status = 1;
subNodeNavigateUrl = "javascript:SelectNode('subsys','" + subSysId + "'," + ConvertToString((nodeid++)) + "," + status + ")";
rootNodeChildNodesAdd(subNode);
rootNodeExpanded = true;
DataTable sdt = new DataTable();
sdt = subSystemGetModulesBySubSysId(subSysId); //显示模块名称
foreach (DataRow sdr in sdtRows)
{
TreeNode sNode = new TreeNode();
string moduleName = sdr["CFunName"]ToString();
sNodeText = moduleName;
string moduleId = sdr["CFunctionId"]ToString();
sNodeValue = moduleId;
status = 1;
sNodeNavigateUrl = "javascript:SelectNode('module','" + moduleId + "'," + ConvertToString((nodeid++)) + "," + status + ")";
subNodeChildNodesAdd(sNode);
subNodeExpanded = true;
DataTable fdt = new DataTable();
fdt = subSystemGetFunctionsByModuleId(moduleId); //显示功能名称
foreach (DataRow fdr in fdtRows)
{
TreeNode fNode = new TreeNode();
fNodeText = fdr["CFunName"]ToString();
fNodeValue = fdr["CFunctionId"]ToString();
status = 2;
fNodeNavigateUrl = "javascript:SelectNode('function','" + fdr["CFunctionId"]ToString() + "'," + ConvertToString((nodeid++)) + "," + status + ")";
sNodeChildNodesAdd(fNode);
sNodeExpanded = false;
}
}
}
}
// 根节点 单位节点
datatable dtNodeDepartment=new datatable();
dtNodeDepartment=dbgetDatatable(" select distinct 单位编号 from department");//该行为读取数据库数据至datatable 表格dtNodeDepartment 中。您根据自己情况调整。
for(int i=0;i<dtNodeDepartmentRowsCount ;i++) //遍历数据表的行,将数据添加进去
{
TreeNode node = new TreeNode();
nodeText = dtNodeDepartmentRows[i][0]ToString()Trim();// 您的需求是对该行数据 根据节点的级别,求day,或求hour,或求year,然后绑定到nodetext就行了。
nodeName = "单位编号";
treeViewNodesAdd(node);
}
祝你成功!!
以上就是关于如何在c# treeview控件动态绑定数据库,实现2级目录,添加父节点并在父节点下添加子节点全部的内容,包括:如何在c# treeview控件动态绑定数据库,实现2级目录,添加父节点并在父节点下添加子节点、如何把数据库里面的数据绑定到Treeview控件中,我用的是C#连得SQL Server、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)