本人vb.net初学者,折腾几天了,恳请哪位热血程序员帮忙,感激之情绵绵无绝期!
经过努力,实现了第一个要求
Public Class fVarious
Dim mycn As New SqlClient.SqlConnection
Dim myadapter As New SqlClient.SqlDataAdapter
Dim mycommand As New SqlClient.SqlCommand
Private Sub fVarious_Load(sender As Object, e As EventArgs) Handles MyBase.Load
mycn.ConnectionString = "server=localhost;database=wks;uid=sa;pwd=0000;"
mycommand.CommandText = "Select wks.treeid,wks.Matname,wks.MATid from series where len(wks.treeid)=1"
mycommand.Connection = mycn
Try
mycn.Close()
mycn.Open()
Dim mysqlreader As Data.SqlClient.SqlDataReader = mycommand.ExecuteReader
TreeView1.Nodes.Clear()
While mysqlreader.Read()
Dim tree_root As New TreeNode
tree_root.Tag = mysqlreader.GetString(0)
tree_root.Text = mysqlreader.GetString(1) + mysqlreader.GetString(2)
TreeView1.Nodes.Add(tree_root)
End While
Catch ex As Exception
MessageBox.Show("treeview1连接数据失败")
Finally
mycn.Close()
End Try
TreeView1.ExpandAll()
TreeView1.Select()
End Sub
------解决思路----------------------
Public Class fVarious
Dim mycn As New SqlClient.SqlConnection
Dim myadapter As New SqlClient.SqlDataAdapter
Dim mycommand As New SqlClient.SqlCommand
Private Sub fVarious_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
mycn.ConnectionString = "server=localhost;database=wks;uid=sa;pwd=0000;"
mycommand.CommandText = "Select treeid,Matname,MATid from MatTree order by treeid" '一次性载入'
mycommand.Connection = mycn
Try
mycn.Close()
mycn.Open()
Dim mysqlreader As Data.SqlClient.SqlDataReader = mycommand.ExecuteReader
TreeView1.Nodes.Clear()
While mysqlreader.Read()
Dim key As String = mysqlreader.GetString(0)
Dim text As String = mysqlreader.GetString(1) & " " & mysqlreader.GetString(2)
If key.Length = 1 Then
TreeView1.Nodes.Add(key, text) '
Else
Dim parentKey As String = key.Substring(0, key.Length - 2)
TreeView1.Nodes(parentKey).Nodes.Add(key, text)
If key.Length <= 3 Then '要求1'
TreeView1.Nodes(parentKey).Expand()
End If
End If
Dim tree_root As New TreeNode
End While
Catch ex As Exception
MessageBox.Show("treeview1连接数据失败")
Finally
mycn.Close()
End Try
'TreeView1.ExpandAll()不要全展开'
TreeView1.Select()
End Sub
Private Sub TreeView1_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
If Not e.Node Is Nothing Then
'要求2'
e.Node.Expand()
'要求3'
Dim a() As String = Split(e.Node.Text, " ", 2)
TextBox1.Text = a(0)
TextBox1.Text = a(1)
End If
End Sub
End Class