Click here to Skip to main content
Click here to Skip to main content

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

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)

Share

About the Author

TL Wallace
Founder Arkitech EBC Corporation
United States United States
Developer, consultant, and trainer that specializes in building data-centric applications designed for small businesses, universities, community & faith based organizations. Started by developing Excel VBA macros in 1989 for businesses and never looked back. Since then I have gone on to author several programs using VB.NET, SQL Server, Microsoft Access, and ASP.net.

Comments and Discussions

 
GeneralMy vote of 2 PinmvpDave Kreskowiak2-Mar-10 3:44 
Generalhm Pinmemberkonikula20-Dec-09 12:43 
GeneralRe: hm PinmemberTL Wallace20-Dec-09 14:47 
Newsvb.net 2005 pro issues PinmemberBarryGSumpter28-Dec-06 9:37 
GeneralError PinmemberJagadishgowda11-Oct-06 23:23 
GeneralRe: Error PinmemberTL Wallace12-Oct-06 4:36 
GeneralErrot Message Pinmembersagarmd16-Jun-06 0:34 
GeneralRe: Errot Message PinmemberTLWallace.NET16-Jun-06 19:04 
QuestionRe: Errot Message [modified] Pinmembersagarmd19-Jun-06 19:55 
GeneralPretty cool Pinmemberglasssd24-Apr-06 7:29 
GeneralRe: Pretty cool PinmemberTLWallace.NET24-Apr-06 13:35 

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.

| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 9 Apr 2006
Article Copyright 2006 by TL Wallace
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid