Solved

Creating treeview from  a dataset VB.net

Posted on 2012-12-30
10
1,847 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 69

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 69

Expert Comment

by:Éric Moreau
ID: 38730811
it is the same thing, you will just need more datatables, relations, for loops
0
 

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 69

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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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 69

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Suggested Solutions

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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

747 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now