[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2445
  • Last Modified:

Creating treeview from a dataset VB.net

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
arozzy
Asked:
arozzy
  • 5
  • 4
1 Solution
 
Éric MoreauSenior .Net ConsultantCommented:
0
 
arozzyAuthor Commented:
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
 
Éric MoreauSenior .Net ConsultantCommented:
it is the same thing, you will just need more datatables, relations, for loops
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
arozzyAuthor Commented:
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
 
Éric MoreauSenior .Net ConsultantCommented:
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
 
jagssiduralaCommented:
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
 
arozzyAuthor Commented:
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
 
Éric MoreauSenior .Net ConsultantCommented:
create 2 other relations
0
 
arozzyAuthor Commented:
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
 
arozzyAuthor Commented:
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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now