Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I need some help using datasets and treeviews. I'm working a small private project that fills a treeview from a database. This works OK using the FillTree() sub below. When a treeview node is clicked I want to be able to fill other controls on my form with data from the same database. I cannot work out how to do this. Can someone please help me complete tvCodeExplore_AfterSelect()

 

Any help would be invaluable

 

Many thanks

 

Russ

 

#Region "   Treeview Code "
   Private Sub FillTree()
       Dim rootAdapter As New OleDbDataAdapter("SELECT * FROM tblSource WHERE ParentID = 0", conn)
       Dim childAdapter As New OleDbDataAdapter("SELECT * FROM tblSource WHERE ParentID <> 0", conn)

       Dim treeDataSet As New DataSet()
       treeDataSet.EnforceConstraints = False
       rootAdapter.Fill(treeDataSet, "Parents")
       childAdapter.Fill(treeDataSet, "Children")
       treeDataSet.Relations.Add("DBtoFolder", treeDataSet.Tables("Parents").Columns("ID"), treeDataSet.Tables("Children").Columns("ParentID"))

       treeDataSet.Relations.Add("FolderToItem", treeDataSet.Tables("Children").Columns("ID"), treeDataSet.Tables("Children").Columns("ParentID"))

       tvCodeExplore.Nodes.Clear()

       Dim parent As DataRow
       For Each parent In treeDataSet.Tables("Parents").Rows
           Dim parentNode As New TreeNode(parent("Name").ToString())

           DIm child As DataRow
           For Each child In parent.GetChildRows("DBtoFolder")
               Dim folderNode As New TreeNode(child("Name").ToString())
               Dim item As DataRow
               For Each item In child.GetChildRows("FolderToItem")
                   folderNode.Nodes.Add(New TreeNode(item("Name").ToString()))
               Next item
               parentNode.Nodes.Add(folderNode)
           Next child
           tvCodeExplore.Nodes.Add(parentNode)
       Next parent

       tvCodeExplore.Nodes(0).Expand()
   End Sub

   Private Sub tvCodeExplore_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tvCodeExplore.AfterSelect
       'If the e.Node.Parent doesnt exist in otherwords...
       If e.Node.Parent Is Nothing Then
           Me.lblTitle.Text = e.Node.Text
           Me.rtbNotes.Rtf = Nothing
           Me.ctbVB.Text = Nothing
           Me.ctbCSharp.Text = Nothing
           Me.ctbCPlus.Text = Nothing
           Me.ctbJava.Text = Nothing
           Me.ctbHTML.Text = Nothing
           Me.TabPage7.Title = Nothing


           'if it Does exist...
       ElseIf Not e.Node.Parent Is Nothing Then
           Me.lblTitle.Text = e.Node.Text
           Me.rtbNotes.Rtf = 'notes column from database to go in here
           Me.ctbVB.Text = 'CodeVB column from database to go in here
           Me.ctbCSharp.Text = 'CodeCS column from database to go in here
           Me.ctbCPlus.Text = 'CodeCPP column from database to go in here
           Me.ctbJava.Text = 'CodeJava column from database to go in here
           Me.ctbHTML.Text = 'CodeHTML column from database to go in here
           Me.TabPage7.Title = 'CodeOtherName column from database to go in here
       End If
   End Sub
#End Region

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...