Solved

Creating treeview from  a dataset VB.net

Posted on 2012-12-30
10
2,034 Views
Last Modified: 2013-01-15
Hi experts


I have two tables

stokmaster     billofmaterials   ==> TABLES

STOKID pk      STOKID FK  ==> COLUMNS
STOKAD          STOKAD
BILLST             ALTSTKID
                        ALTSTKAD

the relationship is that if a stok in stokmaster table has a bill (BOM), its Billst = 1, and there is a list for its bill of materials in billofmaterials table.

When I am  creating a treeview, only I created for parend and its own child, but i want to show child's child and also its grandchild. I mean that all children of any parent have to be shown in treview.

like that

A
   B
   C
   D
      E
         F
  G
      J
        K
           L
           M
               N
           O
  P
  X
  Y
  Z

I need the method for creating multi level parent child trevieew in vb.net.

Thanks

Best Regards

Oguzhan
0
Comment
Question by:arozzy
  • 5
  • 4
10 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 38730775
0
 

Author Comment

by:arozzy
ID: 38730792
thanks fpr reply, but this sample only include a parent and its children, there is no any grandchild and child of grandchild in that sample
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 38730811
it is the same thing, you will just need more datatables, relations, for loops
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:arozzy
ID: 38730827
then how can I create treeview  by querying childnode? has the childnode a child or not? and adding this child down of that childnode and of course. Also this process will continue to end reaching last child of that item ?
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 38730867
a child node is a node. you can add new child nodes to it.

to find out if a node has children or not, you can check YourNode.Nodes.Count > 0
0
 
LVL 12

Expert Comment

by:jagssidurala
ID: 38730995
Refer this links

http://stackoverflow.com/questions/7543873/vb-net-populate-treeview-with-dataset-using-parent-child-relations

Have a look at this: Populating a TreeView Control from the Database[^] it's in C#, but it covers doing exactly what you want. Translating it to VB isn't difficult - there are online translators which do most of it for you: http://www.developerfusion.com/tools/convert/csharp-to-vb/[^]
0
 

Author Comment

by:arozzy
ID: 38731092
THANKS ALL EXPERTS FOR ANSWERS

MY CODE IS BELOW

i CREATE A TWO LEVEL (A PARENT AND A CHILD) TREEVIEW WITH THIS, HOW CAN REACH ALL CHILDREN? PLEASE HELP

adptr2.Fill(dset1, "RCTANA")
        adptr3.Fill(dset1, "STOKANA")
        'Creates a relation between the 2 DataTables
        Dim relPC As DataRelation = dset1.Relations.Add("relSTOKRECETE", _
                                      dset1.Tables("STOKANA").Columns("STOKID"), _
                                      dset1.Tables("RCTANA").Columns("STOKID"))

        With TreeView1
            'prevents the control from painting until the EndUpdate method is called
            .BeginUpdate()
            'Clears/Deletes all nodes that currently exist
            .Nodes.Clear()
            'Loop through all rows of the Categories table
            For Each drSTK As DataRow In dset1.Tables("STOKANA").Rows
                'Adds the Category as a root node
                Dim nodParent As TreeNode
                nodParent = TreeView1.Nodes.Add(drSTK.Item("STOKAD").ToString)
                'Loops through the products related to the Category
                For Each drRCT As DataRow In drSTK.GetChildRows(relPC)
                    'Adds the Product as a child of the Category
                    Dim nodChild As TreeNode
                    nodChild = nodParent.Nodes.Add(drRCT.Item("ALTSTKAD").ToString)
                Next drRCT
            Next drSTK
            'Expands all nodes
            .ExpandAll()
            'Enables the redrawing of the tree view
            .EndUpdate()
            'Ensures that the first node is visible
            .Nodes(0).EnsureVisible()
        End With
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 38731193
create 2 other relations
0
 

Accepted Solution

by:
arozzy earned 0 total points
ID: 38765140
problem is solved with recursion and codes are below

        TreeView1.Update()
        TreeView1.Nodes.Clear()
        Dim parent As TreeNode
        parent = New TreeNode(ComboBox1.Text)
        TreeView1.Nodes.Add(parent)
        AddMoreChildren(parent)

        TreeView1.ExpandAll()
        TreeView1.EndUpdate()
        baglan.Close()
    End Sub
    Public Sub AddMoreChildren(ByVal parent As TreeNode)

        Dim ds As New DataSet
        Dim adptr As New SqlDataAdapter("select * from RCTANA where RCTAD = '" + parent.Text + "'", baglan)
        ds.Clear()
        adptr.Fill(ds)
        For i = 0 To ds.Tables(0).Rows.Count - 1
            If ds.Tables(0).Rows(i).Item("RCTAD").ToString = parent.Text Then
                Dim child As TreeNode = New TreeNode(ds.Tables(0).Rows(i).Item("ALTSTKAD").ToString)
                parent.Nodes.Add(child)
                AddMoreChildren(child)
            End If
        Next
    End Sub
0
 

Author Closing Comment

by:arozzy
ID: 38777597
Dear Experts

Thanks for your helps and I am sorry for this late answer. I had tried for solution since I wrote here last time. I tried adding relationships but it's very hard to create n-level treeview with this method and I tried using a few datasets and this method didnot allow me to have n-level.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question