Click here to Skip to main content
Licence CPOL
First Posted 9 Apr 2006
Views 58,731
Downloads 457
Bookmarked 46 times

Multiple table databinding in TreeView

By | 9 Apr 2006 | Article
This code will demonstrates databinding in a TreeView.

Sample Image

Introduction

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.

Requirements

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
    Try

        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)
        node.Parent.Nodes.Remove(node)
        ReOrderSiblings(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)
    Try
        Select Case e
            Case DataTreeView.EditType.Foreign
                Dim facade As New Database.CarFacade
                facade.DeleteCar(ID)
            Case DataTreeView.EditType.Primary
                Dim facade As New Database.DealerFacade
                facade.DeleteDealer(ID)
        End Select
    Catch ex As Exception

    End Try
End Sub

Additional Credits

History

  • 1.0.0.0: Initial release (09 April 2006)
  • 1.0.0.1: Minor changes (09 April 2006)

License

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

About the Author

TL Wallace

Software Developer (Senior)
Arkitech EBC Corporation
United States United States

Member

I am a developer, consultant, and trainer. I specialize in building data-centric applications designed for small business, community, and faith based organizations. I got started developing Excel VBA macros in 1989 for business and never looked back. Since then I have gone on to author several programs using VB.NET, SQL Server, Microsoft Access, and ASP.net.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 2 PinmvpDave Kreskowiak3:44 2 Mar '10  
Generalhm Pinmemberkonikula12:43 20 Dec '09  
GeneralRe: hm PinmemberTL Wallace14:47 20 Dec '09  
Newsvb.net 2005 pro issues PinmemberBarryGSumpter9:37 28 Dec '06  
GeneralError PinmemberJagadishgowda23:23 11 Oct '06  
GeneralRe: Error PinmemberTL Wallace4:36 12 Oct '06  
GeneralErrot Message Pinmembersagarmd0:34 16 Jun '06  
GeneralRe: Errot Message PinmemberTLWallace.NET19:04 16 Jun '06  
QuestionRe: Errot Message [modified] Pinmembersagarmd19:55 19 Jun '06  
GeneralPretty cool Pinmemberglasssd7:29 24 Apr '06  
GeneralRe: Pretty cool PinmemberTLWallace.NET13:35 24 Apr '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web01 | 2.5.120517.1 | Last Updated 9 Apr 2006
Article Copyright 2006 by TL Wallace
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid