我正在尝试创建一个类似于以下内容的树视图:
父
| __的 Child1
| __ __ __ O 孙子1
| __ __ __ O 孙子2
| __的 CHILD2
| __ __ __ O 孙子3
| __ __ __ O 孙子4
我在Visual Studio 2008中使用vb.net。非常感谢有关如何实现这一点的任何见解!
答案 0 :(得分:1)
您不能在TreeView中使用单选按钮,只能使用复选框。
解决方案是使复选框的行为类似于单选按钮:
Private Sub TreeView1_AfterCheck(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterCheck
If e.Node.Checked Then
If e.Node.Level = 2 Then
For Each node As TreeNode In e.Node.Parent.Nodes
If node IsNot e.Node Then
node.Checked = False
End If
Next
Else
e.Node.Checked = False
End If
End If
End Sub
e.Node.Level = 2
检查确保只有孙子节点的行为类似于单选按钮。
将TreeView的CheckBoxes
属性设置为True
以启用复选框。
这是如何更改所选节点及其父节点的文本样式的示例:
Private Sub TreeView1_BeforeSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView1.BeforeSelect
If TreeView1.SelectedNode IsNot Nothing Then
MakeSelected(TreeView1.SelectedNode, False)
End If
MakeSelected(e.Node, True)
End Sub
Private Sub MakeSelected(ByVal node As TreeNode, ByVal selected As Boolean)
Dim SelectedFont As New Font(TreeView1.Font, FontStyle.Bold)
node.NodeFont = IIf(selected, SelectedFont, TreeView1.Font)
node.ForeColor = IIf(selected, Color.Blue, TreeView1.ForeColor)
If node.Parent IsNot Nothing Then
MakeSelected(node.Parent, selected)
End If
End Sub
它递归地更改所选节点及其父节点的文本样式,并在选择更改时将其设置回TreeView的默认样式。
答案 1 :(得分:0)
Public Sub getAFCategories(selectedId As String)
Dim cn As Data.SqlClient.SqlConnection
cn = New Data.SqlClient.SqlConnection(ConfigurationManager.AppSettings("ConnStr"))
Try
Dim MerchantSiteId As String = get_MerchantSiteID()
Dim da As SqlDataAdapter
Dim dt As New DataTable
Dim cmd As New SqlCommand()
cmd.Connection = cn
cmd.CommandType = CommandType.Text
cmd.CommandText = "SELECT categoryId, Category, ParentId FROM tblCategories"
cn.Open()
da = New SqlDataAdapter(cmd)
da.Fill(dt)
TrViewAfCategories.Nodes.Clear()
Me.PopulateTreeView2(dt, 0, Nothing, dt, selectedId)
Catch ex As Exception
Finally
cn.Close()
End Try
End Sub
Private Sub PopulateTreeView2(dtParent As DataTable, parentId As Integer, treeNode As TreeNode, dtMain As DataTable, selectedId As String)
Dim redindex As Integer = 1
For Each row As DataRow In dtParent.Rows
Dim child As New TreeNode() With { _
.Text = row("Category").ToString(), _
.Value = row("AFCategoryId").ToString() _
}
If row("ParentId").ToString() = 0 Then
TrViewAfCategories.Nodes.Add(child)
redindex = parentId + redindex
Dim dtChild As DataTable = Me.GetFilterData(dtMain, child.Value) 'Me.getData("SELECT AFCategoryId, Category, ParentId FROM tblAFFeedCategories WHERE ParentId = " + child.Value)
PopulateTreeView2(dtChild, redindex, child, dtMain, selectedId)
Else
If parentId = 1 Then
child.ShowCheckBox = False
Dim dtChild As DataTable = Me.GetFilterData(dtMain, child.Value)
If dtChild.Rows.Count = 0 Then
If (selectedId = child.Text) Then
child.Text = "<input type='radio' name='rdoC" + row("AFCategoryId").ToString() + "' value ='" + row("Category").ToString() + "' checked='checked'/>" + child.Text
Else
child.Text = "<input type='radio' name='rdoC" + row("AFCategoryId").ToString() + "' value ='" + row("Category").ToString() + "' />" + child.Text
End If
End If
treeNode.ChildNodes.Add(child)
'Me.getData("SELECT AFCategoryId, Category, ParentId FROM tblAFFeedCategories WHERE ParentId = " + child.Value)
PopulateTreeView2(dtChild, parentId, child, dtMain, selectedId)
End If
End If
Next
End Sub
Private Function GetFilterData(parentData As DataTable, filterValue As String) As DataTable
Dim dt As New DataTable()
Dim dvFilter As New DataView
dvFilter = parentData.DefaultView
dvFilter.RowFilter = Nothing
dvFilter.RowFilter = "ParentId = " & filterValue
dt = dvFilter.ToTable()
Return dt
End Function