foreach (ModelItem nowmodelItem in root.Children)里面root 也是ModelItem 类型,他是根,一定有子孩子,但是他的孩子就不一定有孩子了,该怎么搞~~~~~不要复制一大堆过来啊~~~~用的多少datasourse看不懂啊,我这个不是datasourse 就是
一个ModelItem 类而已~~if (modelItem.Children == null).....else...........就这样帮忙写一下啊~~~treeview~~~

解决方案 »

  1.   

    void BuildTree(ModelItem item){
    //输出item
    foreach (ModelItem nowmodelItem in item.Children){
    BuildTree(nowmodelItem);//这里递归
    }
    }调用:
    BuildTree(root)
      

  2.   


    public void AddTree(int ParentID, TreeNode pNode) 
        { 
            TreeNode tn1 = new TreeNode(); 
            DataView dvTree = new DataView(ds.Tables[0]); 
            //过滤ParentID,得到当前的所有子节点 
            dvTree.RowFilter = "[PARENTID] = " + ParentID; 
            foreach (DataRowView Row in dvTree) 
            { 
                if (pNode == null) 
                {    //''?添加根节点 
      
                    tn1.Text = Row["name"].ToString(); 
                    TreeView1.Nodes.Add(tn1); 
                    tn1.Expanded = true; 
                    AddTree(Int32.Parse(Row["ID"].ToString()), tn1);    //再次递归 
                } 
                else 
                {   //添加当前节点的子节点 
                    TreeNode tn2 = new TreeNode(); 
                    tn2.Text = Row["name"].ToString(); 
                    //pNode.Nodes.Add(tn2); 
                    pNode.ChildNodes.Add(tn2); 
                    tn1.Expanded = true; 
                    AddTree(Int32.Parse(Row["ID"].ToString()), tn2);    //再次递归 
                } 
      
            } 
      
      
      

  3.   

    Private Property AccFunDataSet As DataSet
      Get
      Dim ds As DataSet  If Session("AccFunctionsDataSet") Is Nothing Then
      Dim comm As New Common()
      Dim CodeMain As New MTRC.IRIS.DAL.codeMaintenance()  Try
      ds = CodeMain.getAccFunctions("", Me.curLanguage)
      Dim dr As DataRow = ds.Tables(0).NewRow
      dr("AccessFunctionID") = ""
      ds.Tables(0).Rows.Add(dr)  Session("AccFunctionsDataSet") = ds
      Catch ex As Exception
      comm.LogEvent(ex.Message, Diagnostics.EventLogEntryType.Error)
      Throw
      Finally
      CodeMain = Nothing
      comm = Nothing
      End Try  Else
      ds = Session("AccFunctionsDataSet")  End If  Return ds
      End Get
      Set(ByVal value As DataSet)
      Session("AccFunctionsDataSet") = Nothing
      End Set
      End Property
      Private Function GetSubAccFunctions(ByVal ParentFunctionID As String, Optional ByVal IncludeEmptyRow As Boolean = False) As DataView  Dim ds As DataSet = AccFunDataSet
      Dim filter As String = "AccessFunctionID='" + ParentFunctionID + "'"
      If IncludeEmptyRow Then
      filter += " Or AccessFunctionID=''"
      End If  Dim dv As DataView = New DataView(ds.Tables(0), filter, "AccessFunctionID ASC", DataViewRowState.CurrentRows)  Return dv  End Function  Private Sub ShowTree()  tvwAccFunction.Nodes.Clear()  Dim RootNode As New System.Web.UI.WebControls.TreeNode()
      RootNode.Text = "IRIS"
      RootNode.Value = "-"  tvwAccFunction.Nodes.Add(RootNode)
      CreateChildTree(RootNode)  RootNode.Select()  End Sub Private Sub CreateChildTree(ByVal ParentNode As System.Web.UI.WebControls.TreeNode)  ParentNode.Expanded = False
      Try
      Dim dv As DataView = GetSubAccFunctions(ParentNode.Value)
      Dim i As Integer
      For i = 0 To dv.Count - 1
      Dim myTreeNode As New System.Web.UI.WebControls.TreeNode()
      myTreeNode.Text = dv(i)("AccessFunctionDesc")
      myTreeNode.Value = dv(i)("AccessSubFunctionID")  ParentNode.ChildNodes.Add(myTreeNode)  If dv(i)("HasChildren") Then
      CreateChildTree(myTreeNode)
      End If  Next
      Catch ex As Exception
      Throw
      Finally  End Try
      End Sub  Protected Sub tvwAccFunction_SelectedNodeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles tvwAccFunction.SelectedNodeChanged
      'e.Node
      Dim node As TreeNode = tvwAccFunction.SelectedNode  AccFunction = node.Value
      BindGrid(node.Value)  Me.tvwAccFunction.CollapseAll()  SelectedValuePath = node.ValuePath  ExpandTreeNode(node)
      End Sub  Private Sub ExpandTreeNode(ByVal valuePath As String)
      Dim node As TreeNode = tvwAccFunction.FindNode(valuePath)
      If Not node Is Nothing Then
      ExpandTreeNode(node)
      End If
      End Sub  Private Sub ExpandTreeNode(ByVal node As TreeNode)
      Dim vp As String = node.ValuePath
      Dim pos As Integer = -1
      pos = vp.IndexOf(tvwAccFunction.PathSeparator, pos + 1)
      While pos > 0
      Dim nd As TreeNode = tvwAccFunction.FindNode(vp.Substring(0, pos))
      nd.Expanded = True  pos = vp.IndexOf(tvwAccFunction.PathSeparator, pos + 1)
      End While  node.Expanded = True
      node.Selected = True
      End Sub
      

  4.   

    里面root 也是ModelItem 类型,他是根,一定有子孩子,但是他的孩子就不一定有孩子了,该怎么搞~~~~~
    你可以这样想,不知道他的孩子有没有孩子的办法就是去寻找有没有以他为父亲的孩子集合,如果有Item是以他为父亲的,那么他肯定有孩子的
      

  5.   

    巧了,刚才在做项目的时候就写了一个,O(∩_∩)O哈哈~
      ArrayList al = new ArrayList();
        //获取选中节点的文本集合
        private void NavigateTreeView(TreeView treeview)
        {
            foreach (TreeNode tn in treeview.Nodes)
            {
                FindNodes(tn);
            }
        }    private void FindNodes(TreeNode node)
        {
            try
            {
                if (node.ChildNodes.Count > 0)
                {
                    foreach (TreeNode n in node.ChildNodes)
                    {
                        FindNodes(n);
                    }
                }
                else
                {
                    if (node.Checked == true)
                    {
                        al.Add(node.Text);
                    }
                }
            }
            catch (Exception)
            {
                
            }
        }