Solved

Creating treeview from  a dataset VB.net

Posted on 2012-12-30
10
2,140 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
I have a large data set and a SSIS package. How can I load this file in multi threading?
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.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

705 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