如果复选框显示在 TreeView 控件中(通过将 ShowCheckBoxes 属性设置为 TreeNodeType.None 以外的值),可使用 CheckedNodes 属性确定哪些节点显示选中了的复选框。此集合通常用于循环访问树中所有选中了复选框的节点。

解决方案 »

  1.   

    显示动态绑定,(vb.net)写法TreeView2.Nodes.Clear()
            Dim conn As String = System.Configuration.ConfigurationManager.ConnectionStrings("znhConnectionStringall").ToString
            Dim sqlf As String = "select * from mainmenu"        Dim daf As New SqlDataAdapter(sqlf, conn)
            Dim dsf As New DataSet
            daf.Fill(dsf)
            Dim i As Integer
            For i = 0 To dsf.Tables(0).Rows.Count - 1
                Dim td1 As New TreeNode
                td1.Text = dsf.Tables(0).Rows(i).Item(1).ToString
                TreeView2.Nodes.Add(td1)
                td1.Checked = True            Dim sqlc As String = "select * from childmenu where menu_id=" & dsf.Tables(0).Rows(i).Item(0) & ""
                Dim da2 As New SqlDataAdapter(sqlc, conn)
                Dim ds2 As New DataSet
                da2.Fill(ds2)
                Dim j As Integer
                For j = 0 To ds2.Tables(0).Rows.Count - 1
                    Dim td2 As New TreeNode
                    td2.Text = ds2.Tables(0).Rows(j).Item(2).ToString
                    td1.ChildNodes.Add(td2)                Dim sqlx As String = "select * from usermenu where [name]='" & DropDownList2.SelectedItem.ToString & "'"
                    Dim dax As New SqlDataAdapter(sqlx, conn)
                    Dim dsx As New DataSet
                    dax.Fill(dsx)
                    Dim a As Integer
                    For a = 0 To dsx.Tables(0).Rows.Count - 1
                        If td2.Text = dsx.Tables(0).Rows(a).Item(2).ToString Then
                            td2.Checked = True                    End If
                    Next            Next
            Next//这是保存checked数据
    Dim conn1 As SqlConnection
            Dim connstr As String
            Dim comm As SqlCommand
            Dim commdel As SqlCommand
            connstr = System.Configuration.ConfigurationManager.ConnectionStrings("znhConnectionStringall").ToString
            conn1 = New SqlConnection(connstr)
            Dim sql As String = "select max(admin_qx) from admin"
            Dim adapter As New SqlDataAdapter(sql, conn1)
            Dim dataset As New DataSet
            adapter.Fill(dataset)        'Response.Write(dataset.Tables(0).Rows(0).Item(0))
            ' Response.End()        Dim a As Integer
            a = dataset.Tables(0).Rows(0).Item(0) + 1
            ' Response.Write(a)
            ' Response.End()
            conn1.Open()
            commdel = New SqlCommand("delete usermenu where name='" & DropDownList2.SelectedItem.ToString & "'", conn1)
            commdel.ExecuteNonQuery()        For Each node As TreeNode In TreeView2.CheckedNodes
                'Label1.Text += "<li> " & node.Value
                comm = New SqlCommand("insert into usermenu (usermenu,qx,name) values('" & node.Value & "','" & a & "','" & DropDownList2.SelectedItem.ToString & "')", conn1)
                comm.ExecuteNonQuery()
            Next
            conn1.Close()        conn1.Open()
            Dim comm1 As SqlCommand
            comm1 = New SqlCommand("update admin set admin_qx='" & a & "' where admin_name='" & DropDownList2.SelectedItem.ToString & "' ", conn1)
            comm1.ExecuteNonQuery()
            conn1.Close(),
      

  2.   

    在AfterCheck事件中:if(e.Node.Checked == true)   
    {       
    if(e.Node.Nodes.Count > 0)
    {
    for(int i = 0; i < e.Node.Nodes.Count; i++)   
    {   
    e.Node.Nodes[i].Checked = true;     
    }   
    }
    }   
    else   
    {   
    if(e.Node.Nodes.Count > 0)
    {
    for(int i = 0; i < e.Node.Nodes.Count; i++)   
    {   
    .Node.Nodes[i].Checked = false;     
    }     
    }
    }