In web application some times we have a need to retieve the data from the database and displayed through treeview. For that need following coding will be used.
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim con As New SqlConnection("server=VAXGEN-3;uid=sa;pwd=vgn;database=Northwind")
Dim ds, dst As New DataSet Dim dt, dt1 As DataTable Dim dr As SqlDataReader
Dim da As New SqlDataAdapter("select name from sysobjects where type='U'", con) Dim count As Integer
con.Open() da.Fill(ds, "table1") con.Close() dt = ds.Tables(0) count = dt.Rows.Count
For Each drow As DataRow In dt.Rows Dim name As String name = drow(0).ToString()
Me.TreeView2.ShowCheckBoxes = TreeNodeTypes.Parent
Dim table As New TreeNode(name) table.Value = name
rootnode.ChildNodes.Add(table)
Dim cmd As New SqlCommand("select * from " + name + "", con)
con.Open() da.SelectCommand = cmd da.Fill(dst, name) dt = dst.Tables(name)
Dim ct As Integer = dt.Columns.Count()
For i As Integer = 0 To ct - 1 Dim column As New TreeNode(dt.Columns(i).ColumnName.ToString()) table.ChildNodes.Add(column) Next con.Close()
Next
End Sub
AttachmentsOutput For treeview (20831-9108-treeimg.doc)
|
| Author: sathya 10 Sep 2008 | Member Level: Silver Points : 0 |
Hi kalaimanichozhan
This is very useful for me.Good!
|