您的位置:首页 > 编程语言 > VB

[VB.NET]VB中treeView问题,添加节点问题

2008-12-29 23:16 501 查看



<script type="text/javascript"><!--
google_ad_client = "pub-8333940862668978";
/* 728x90, 创建于 08-11-30 */
google_ad_slot = "4485230109";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

VB中treeView问题,添加节点问题
Public Function NodeLevel(ByVal n As TreeNode) As Byte
找出树中当前节点的级数
Dim i As Byte = 1
Dim m As String
Do Until n.Parent Is Nothing
n = n.Parent
i += 1
Loop
Return i
End Function

添加结点
Public Sub fill_treeleaf(ByVal sql As String, ByVal _Name As String)
Dim DS As New DataSet
Dim i As Integer
DS = GetDataFromDB(sql)
Try
If DS Is Nothing Then
Else
For i = 0 To DS.Tables(0).Rows.Count - 1
Dim tree_leaf As New TreeNode
tree_leaf.Text = DS.Tables(0).Rows(i)(_Name)
tree_leaf.Tag = DS.Tables(0).Rows(i)(_Name)
Me.dbtreeView.SelectedNode.Nodes.Add(tree_leaf)
Next
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

查找要添加的结点
Private Sub dbtreeView_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles dbtreeView.AfterSelect
Dim SQLStr As String
Select Case NodeLevel(e.Node).ToString
Case 1
If e.Node.GetNodeCount(False) = 0 Then
If e.Node.Tag Is Nothing Then
Else
SQLStr = Select Distinct(floorNum) From I_Room Where Floorid= & e.Node.Tag
fill_treeleaf(SQLStr, floorNum )
End If
End If
Case 2
If e.Node.GetNodeCount(False) = 0 Then
If e.Node.Tag Is Nothing Then
Else
SQLStr = Select Distinct(floorRoom) From I_Room Where FloorNum= & e.Node.Tag
fill_treeleaf(SQLStr, FloorRoom )
End If
End If
End Select
End Sub

登录时的初始结点
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim i As Integer
Dim ds As New DataSet
Dim SQLstr As String
SQLstr = select floorid,floorName from I_floorinfo
ds = GetDataFromDB(SQLstr)
If ds Is Nothing Then
Else
For i = 0 To ds.Tables(0).Rows.Count - 1
Dim tree_root As New TreeNode
tree_root.Text = ds.Tables(0).Rows(i)( floorname )
tree_root.Tag = ds.Tables(0).Rows(i)( floorid )
Me.dbtreeView.Nodes.Item(0).Nodes.Add(tree_root) (这里要是用Me.dbtreeView.Nodes.Add(tree_root)再点初始的结点时就能,出现子结点,要是用上面的就出不了子结点)
Next
End If
End Sub

我先在treeView中直接加了一根ROOT结点,然后在登录时把ROOT结点下的几个子节点加上来,可是我再点子结点后就没有子结点现示了,要是把初始的子结点,加到ROOT同级时,再点初妈的子结点时,就能显示初始子结点下面的子结点
__________________________________________________________________________
比较乱
__________________________________________________________________________
TreeView 不会有问题的,应该是你添加节点时候的问题,你在添加每个节点的时候输出它的级数和序号,看看是不是逻辑上面出问题了
__________________________________________________________________________
谢谢neil_CN 是我的级数有问题,把case里的1和2改成2和3就行
__________________________________________________________________________
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息