问题描述
有两张表一张表A有NodeIDChildrenIDOrderNum另一张表B为NodeIDtypeNameA中childrenID与B中NodeID相对应A中Nodeid为0代表根节点一行中ChildrenID为8名字在B中显示为散货船A中第二行NodeID为8childrenID为9即为第二级节点名称为H2558A中第三行NodeID为9childrenID为10即为第三级节点名称为尾部机舱区域A中第四行NodeID为10childrenID为11即为第四级节点名称为10AA中第五行NodeID为11childrenID为12即为第五级节点名称为102问题对于这种递归的结构树我怎么获取第五级子节点在定位到第五级子节点时对子节点进行添加属性属性为102等的基本长宽高等基础数据并且将第五级上面四级的名称都能获取到程序代码如下结构树显示代码ImportsSystem.DataImportsSystem.Data.SqlClientPublicClass递归结构树'绑定根节点SubBindRoot()conn1.Close()conn1.Open()DimstrSQLAsStringDimadapterAsSqlDataAdapterDimcmdAsSqlCommandBuilderDimtableAsNewDataTable()DimMaterialInfAsNewDataSetstrSQL="SELECT*FROMF_ProductInfoNodeChildrenList"adapter=NewSqlDataAdapter(strSQL,conn1)cmd=NewSqlCommandBuilder(adapter)DimrowsAsDataRow()adapter.Fill(MaterialInf,"F_ProductInfoNodeChildrenList")table=MaterialInf.Tables(0)rows=table.[Select]("NodeID=0")'取根ForEachdRowAsDataRowInrowsDimrootNodeAsNewTreeNode()rootNode.Tag=dRowDimstrSQL1AsStringDimadapter1AsSqlDataAdapterDimcmd1AsSqlCommandBuilderDimtable1AsNewDataTable()DimMaterialInf1AsNewDataSetstrSQL1="SELECT*FROMF_ProductInfoNodeListwhereNodeID='"&dRow("ChildrenNodeID").ToString()&"'"adapter1=NewSqlDataAdapter(strSQL1,conn1)cmd1=NewSqlCommandBuilder(adapter1)adapter1.Fill(MaterialInf1,"F_ProductInfoNodeList")table1=MaterialInf1.Tables(0)rootNode.Text=table1.Rows(0).Item("Name").ToString()TreeView1.Nodes.Add(rootNode)Iftable1.Rows(0).Item("TypeID").ToString()=1ThenrootNode.ImageIndex=0rootNode.SelectedImageIndex=0ElserootNode.ImageIndex=1rootNode.SelectedImageIndex=1EndIfBindChildAreas(rootNode)NextEndSub'递归绑定子区域SubBindChildAreas(ByValfNodeAsTreeNode)'父节点IDDimstrSQLAsStringDimadapterAsSqlDataAdapterDimcmdAsSqlCommandBuilderDimtableAsNewDataTable()DimMaterialInfAsNewDataSetstrSQL="SELECT*FROMF_ProductInfoNodeChildrenList"adapter=NewSqlDataAdapter(strSQL,conn1)cmd=NewSqlCommandBuilder(adapter)DimrowsAsDataRow()adapter.Fill(MaterialInf,"F_ProductInfoNodeChildrenList")table=MaterialInf.Tables(0)rows=table.[Select]("NodeID="&fNode.Tag(1))'子区域Ifrows.Length=0Then'递归终止,区域不包含子区域时ReturnEndIfForEachdRowAsDataRowInrowsDimnodeAsNewTreeNode()node.Tag=dRowDimstrSQL1AsStringDimadapter1AsSqlDataAdapterDimcmd1AsSqlCommandBuilderDimtable1AsNewDataTable()DimMaterialInf1AsNewDataSetstrSQL1="SELECT*FROMF_ProductInfoNodeListwhereNodeID='"&dRow("ChildrenNodeID").ToString()&"'"adapter1=NewSqlDataAdapter(strSQL1,conn1)cmd1=NewSqlCommandBuilder(adapter1)adapter1.Fill(MaterialInf1,"F_ProductInfoNodeList")table1=MaterialInf1.Tables(0)node.Text=table1.Rows(0).Item("Name")'添加子节点fNode.Nodes.Add(node)'递归BindChildAreas(node)Iftable1.Rows(0).Item("TypeID")=1Thennode.ImageIndex=0node.SelectedImageIndex=0Elsenode.ImageIndex=1node.SelectedImageIndex=1EndIfNextEndSubPrivateSubTreeView1_AfterSelect(senderAsObject,eAsTreeViewEventArgs)HandlesTreeView1.AfterSelectTreeView1.ContextMenuStrip=ContextMenuStrip2EndSubPrivateSub结构树_Load(senderAsObject,eAsEventArgs)HandlesMyBase.LoadTreeView1.Nodes.Clear()TreeView1.ImageList=ImageList1BindRoot()EndSubPrivateSub刷新ToolStripMenuItem_Click(senderAsObject,eAsEventArgs)Handles刷新ToolStripMenuItem.ClickTreeView1.Nodes.Clear()TreeView1.ImageList=ImageList1BindRoot()EndSubPrivateSub增加ToolStripMenuItem_Click(senderAsObject,eAsEventArgs)Handles增加ToolStripMenuItem.Click递归添加.ShowDialog()EndSubEndClass
解决方案
解决方案二:
解决方案三:
if(TreeNode.Level==N)在SQL数据源中判断(CTE需要SQL2005及以上)参考例子E