如何在c# treeview控件动态绑定数据库,实现2级目录,添加父节点并在父节点下添加子节点
如何在c#treeview控件动态绑定数据库,实现2级目录,添加父节点并在父节点下添加子节点。数据库是sqlite,软件是visualstudio...
如何在c# treeview控件动态绑定数据库,实现2级目录,添加父节点并在父节点下添加子节点 。数据库是sqlite,软件是visual studio
展开
2个回答
展开全部
从你的数据表中读出父子节点表到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;
}
}
}
}
给你一个示例,大概是这样子。
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;
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |