Solved

Creating treeview from  a dataset VB.net

Posted on 2012-12-30
10
2,109 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
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

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Display Date and Time 7 47
MDT Create a bootable media for BIOS 2 20
sql how to insert a column with the data  in an existing table 4 28
HIghlights of SSIS? 3 40
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

740 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