
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:
- Data bind a
DataSet to the TreeView and save the results back to a Microsoft Access database.
- Design the solution to make it easy to reuse the class in different solutions.
- Encapsulate as much of the binding logic inside the custom solution and hide it from the UI developers.
- UI developers should have to write next to nothing for code to keep the
TreeView in sync with the DataSet underneath it.
- Provide a sample for deleting nodes in the
TreeView (via a right click context menu) and have that deletion automatically reflected in the DataSet.
- 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.
- 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.
- Support drag and drop of
TreeNode branches within the same TreeView and have the relationships automatically reflected in the DataSet.
- 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.
- Provide a sample for accepting and rejecting changes made to the same
TreeView control, giving the user the ability to undo their changes.
- 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
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)