Click here to Skip to main content
13,199,296 members (68,435 online)
Click here to Skip to main content
Add your own
alternative version


46 bookmarked
Posted 9 Apr 2006

Multiple table databinding in TreeView

, 9 Apr 2006
Rate this:
Please Sign up or sign in to vote.
This code will demonstrates databinding in a TreeView.

Sample Image


At the time of this writing, the standard Windows Forms .NET TreeView control doesn't support the concept of databinding like the ASP.NET TreeView control. So, we are left writing our own solutions. This solution provides a standardized way to use data binding with the Windows Forms .NET TreeView control. This functionality will enable you to quickly develop user-friendly ways to create and view complex data information. The solution includes the data binding implementation as well as the following Design Patterns: Facade, Proxy, Singleton. It also includes custom 'Rule' classes to give the developer, at design time, the ability to derive primary key and foreign key data points. This is essential to adjusting the underlying dataset when changes to the TreeView occur. Updates are also performed on a separate asynchronous thread. The last item I'd like to mention is that this class does support two table datasets, a Primary key table and a Foreign key table.


The following were the initial requirements:

  1. Data bind a DataSet to the TreeView and save the results back to a Microsoft Access database.
  2. Design the solution to make it easy to reuse the class in different solutions.
  3. Encapsulate as much of the binding logic inside the custom solution and hide it from the UI developers.
  4. UI developers should have to write next to nothing for code to keep the TreeView in sync with the DataSet underneath it.
  5. Provide a sample for deleting nodes in the TreeView (via a right click context menu) and have that deletion automatically reflected in the DataSet.
  6. Provide a sample for editing nodes in the TreeView (via a right click context menu) and have the Text property change automatically reflected in the DataSet.
  7. Provide a sample for inserting nodes in the TreeView (via a right click context menu) and have the new node automatically reflected in the DataSet.
  8. Support drag and drop of TreeNode branches within the same TreeView and have the relationships automatically reflected in the DataSet.
  9. Support drag and drop of TreeNode branches across multiple TreeView controls and have the relationships automatically reflected in the target DataSet as well as the source DataSet.
  10. Provide a sample for accepting and rejecting changes made to the same TreeView control, giving the user the ability to undo their changes.
  11. Automatically commit changes to the source and target DataSet when a TreeNode branch is dropped on the target TreeView.

Using the code

The DataTreeView included in the attachment allows for the reordering of TreeNodes in the control. Below is a sample of the NudgeUp method:

Public Sub NudgeUp(ByVal node As TreeNode)
    Dim NewIndex As Integer = 0
    Dim NodeClone As TreeNode = Nothing
    If node.Parent Is Nothing Then Exit Sub

        If node Is Nothing Then Return
        If node.Index = 0 Then Return

        NewIndex = node.Index - 1
        NodeClone = CType(node.Clone(), TreeNode)
        node.Parent.Nodes.Insert(NewIndex, NodeClone)
        NodeClone.TreeView.SelectedNode = NodeClone

    Catch ex As Exception
        Throw New Exception("Error occured: NudgeUp")
    End Try
End Sub 'NudgeUp

A brief description follows below of how the UI developer would delete a row using the Facade Design Pattern.

Private Sub OnDeleteNode(ByVal e As DataTreeView.EditType, ByVal ID As Integer)
        Select Case e
            Case DataTreeView.EditType.Foreign
                Dim facade As New Database.CarFacade
            Case DataTreeView.EditType.Primary
                Dim facade As New Database.DealerFacade
        End Select
    Catch ex As Exception

    End Try
End Sub

Additional Credits


  • Initial release (09 April 2006)
  • Minor changes (09 April 2006)


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Terence Wallace
Founder Arkitech EBC Corporation
United States United States
MS, BBA, software developer, consultant, and trainer. Specializing in building data-centric applications designed for business, university, community & faith based organizations. Started developing Excel VBA macros and never looked back. Freelance developer utilizing VB.Net, SQL Server, Microsoft Access, and ASP.Net.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 2 Pin
Dave Kreskowiak2-Mar-10 3:44
mvpDave Kreskowiak2-Mar-10 3:44 
Generalhm Pin
konikula20-Dec-09 12:43
memberkonikula20-Dec-09 12:43 
GeneralRe: hm Pin
TL Wallace20-Dec-09 14:47
memberTL Wallace20-Dec-09 14:47 2005 pro issues Pin
BarryGSumpter28-Dec-06 9:37
memberBarryGSumpter28-Dec-06 9:37 
GeneralError Pin
Jagadishgowda11-Oct-06 23:23
memberJagadishgowda11-Oct-06 23:23 
GeneralRe: Error Pin
TL Wallace12-Oct-06 4:36
memberTL Wallace12-Oct-06 4:36 
GeneralErrot Message Pin
sagarmd16-Jun-06 0:34
membersagarmd16-Jun-06 0:34 
GeneralRe: Errot Message Pin
TLWallace.NET16-Jun-06 19:04
memberTLWallace.NET16-Jun-06 19:04 
QuestionRe: Errot Message [modified] Pin
sagarmd19-Jun-06 19:55
membersagarmd19-Jun-06 19:55 
GeneralPretty cool Pin
glasssd24-Apr-06 7:29
memberglasssd24-Apr-06 7:29 
GeneralRe: Pretty cool Pin
TLWallace.NET24-Apr-06 13:35
memberTLWallace.NET24-Apr-06 13:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171020.1 | Last Updated 9 Apr 2006
Article Copyright 2006 by Terence Wallace
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid