数据库内容用treeview显示问题
数据库的某表中内容是分级的类别要用treeview显示出来各类别及所属关系,求源码 --------------------编程问答-------------------- 请给出数据表的结构。 --------------------编程问答-------------------- 我只需要一个类似的示例 --------------------编程问答-------------------- 我给你写一个你看看能不能帮到你什么忙
在最最上面写上
Imports System
Imports System.data.sqlclient
在一个button的click的上面写上
Dim sqlstr as string="Data Source=(local);Initial Catalog=xk;Integrated Security=SSPI"
Dim myconnection1 as new sqlconnection
Dim myconnection2 as new sqlconnection
在button的click的里面写上
myconnection1.connectionstring=sqlstr
myconnection2.connectionstring=sqlstr
myconnection1.open
myconnection2.open
Dim sqlstr as string="select stuno from student order by stuno asc"
Dim mycommand as new sqlcommand(sqlstr,myconnection1)
Dim mydatareader as datareader
mydatareader=mycommand.executereader
while mydatareader.read
Dim myrow as datarow
Dim n1 as new treenode(mydatareader(0))
Dim str as string="select * from stucou where stuno='" & mydatareader(0) &"'"
Dim mydataadapter as new dataadapter(str,myconnection2)
Dim mydataset as new dataset
mydataadapter.fill(mydataset,"表")
for each myrow in mydataset.tables("表").rows
Dim n2 as new treenode(myrow("cusno"))
Dim ii as integer
for ii=2 to 3
Dim n3 as new treenode(myrow(ii))
n2.nodes.add(n3)
next
n1.nodes.add(n2)
next
treeview1.nodes.add(n1)
end while
myconnection1.close
myconnection2.close
其实你可能不太清楚为什么要有两个sqlconnection
如果你用一个的话就会报错
(忘了报什么错了)但是主要是由datareader 和dataadapter 引起的
所以要用两个sqlconnection
还有就要看你什么数据库
我用的表可能和你的表不一样这点你要随机应变了
你看看吧
--------------------编程问答-------------------- Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim dt As New DataTable
dt.Columns.Add("AutoId", Type.GetType("System.String"))
dt.Columns.Add("DepName", Type.GetType("System.String"))
dt.Columns.Add("FatherId", Type.GetType("System.String"))
For i As Int32 = 0 To 4
Dim dr As DataRow = dt.NewRow
dt.Rows.Add(dr)
Next
For i As Int32 = 1 To 5
dt.Rows(i - 1)(0) = i
Next
dt.Rows(0)(1) = "管理部"
dt.Rows(1)(1) = "经营部"
dt.Rows(2)(1) = "开发部"
dt.Rows(3)(1) = "硬件开发部"
dt.Rows(4)(1) = "软件开发部"
dt.Rows(0)(2) = 0
dt.Rows(1)(2) = 0
dt.Rows(2)(2) = 0
dt.Rows(3)(2) = 3
dt.Rows(4)(2) = 3
Me.DataGridView1.DataSource = dt
For i As Int32 = 0 To 4
If dt.Rows(i)(2) = 0 Then ' 加入根节点
TreeView1.Nodes.Add(CStr(dt.Rows(i)(0)), CStr(dt.Rows(i)(1)))
Else ' 加入子节点
TreeView1.Nodes(CStr(dt.Rows(i)(2))).Nodes.Add(CStr(dt.Rows(i)(0)), CStr(dt.Rows(i)(1)))
End If
Next
End Sub
End Class
--------------------编程问答-------------------- 这一段可能会好一点,但是会有错,请高手看一下:在添加第三行“甲”那一行就会报错:Object reference not set to an instance of an object.
不知道为什么,不知道怎么改?
' 建一个数据源
Dim dt As New DataTable
dt.Columns.Add("ID")
dt.Columns.Add("Text")
dt.Columns.Add("ParentID")
For i As Integer = 0 To 7
Dim dr As DataRow = dt.NewRow
dt.Rows.Add(dr)
Next
dt.Rows(0).ItemArray = New String() {"0", "总经理", "-1"}
dt.Rows(1).ItemArray = New String() {"1", "部门经理A", "0"}
dt.Rows(2).ItemArray = New String() {"2", "甲", "1"}
dt.Rows(3).ItemArray = New String() {"3", "乙", "1"}
dt.Rows(4).ItemArray = New String() {"4", "部门经理B", "0"}
dt.Rows(5).ItemArray = New String() {"5", "丙", "4"}
dt.Rows(6).ItemArray = New String() {"6", "丁", "4"}
dt.Rows(7).ItemArray = New String() {"7", "戊", "4"}
'Me.DataGridView1.DataSource = dt
For i As Integer = 0 To dt.Rows.Count - 1
If dt.Rows(i)(2) = -1 Then
Me.TreeView1.Nodes.Add(CStr(dt.Rows(i)(0)), CStr(dt.Rows(i)(1)))
Else
Me.TreeView1.Nodes(CStr(dt.Rows(i)(2))).Nodes.Add(CStr(dt.Rows(i)(0)), CStr(dt.Rows(i)(1)))
End If
Next
Me.TreeView1.ExpandAll() --------------------编程问答-------------------- 谢谢 --------------------编程问答-------------------- 好好学习~~~ --------------------编程问答-------------------- strong
补充:.NET技术 , VB.NET