Click here to Skip to main content
15,880,503 members
Articles / Desktop Programming / Windows Forms

Multiple table databinding in TreeView

Rate me:
Please Sign up or sign in to vote.
1.82/5 (13 votes)
9 Apr 2006CPOL2 min read 91.2K   1.5K   46   11
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:

VB
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.

VB
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)


Written By
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.

Comments and Discussions

 
GeneralMy vote of 2 Pin
Dave Kreskowiak2-Mar-10 3:44
mveDave Kreskowiak2-Mar-10 3:44 
Generalhm Pin
konikula20-Dec-09 12:43
konikula20-Dec-09 12:43 
GeneralRe: hm Pin
Terence Wallace20-Dec-09 14:47
Terence Wallace20-Dec-09 14:47 
Newsvb.net 2005 pro issues Pin
BarryGSumpter28-Dec-06 9:37
BarryGSumpter28-Dec-06 9:37 
GeneralError Pin
Jagadishgowda11-Oct-06 23:23
Jagadishgowda11-Oct-06 23:23 
GeneralRe: Error Pin
Terence Wallace12-Oct-06 4:36
Terence Wallace12-Oct-06 4:36 
GeneralErrot Message Pin
sagarmd16-Jun-06 0:34
sagarmd16-Jun-06 0:34 
GeneralRe: Errot Message Pin
Terence Wallace16-Jun-06 19:04
Terence Wallace16-Jun-06 19:04 
QuestionRe: Errot Message [modified] Pin
sagarmd19-Jun-06 19:55
sagarmd19-Jun-06 19:55 
GeneralPretty cool Pin
glasssd24-Apr-06 7:29
glasssd24-Apr-06 7:29 
GeneralRe: Pretty cool Pin
Terence Wallace24-Apr-06 13:35
Terence Wallace24-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.