Click here to Skip to main content
14,029,830 members
Click here to Skip to main content
Add your own
alternative version


15 bookmarked
Posted 4 Oct 2008
Licenced CPOL

Binding a ListView to a TreeView

, 4 Oct 2008
Rate this:
Please Sign up or sign in to vote.
This ListView acts like explorer but based on a pre-defined TreeView



Binding different controls with similar data types can effectively decrease the time required for coding. This can also help different programmers to face similar problems during their coding and they instantly find the solution to their questions in result. Although the data binding methods and approaches are developed in different frameworks but there still remain some unexpected demands for data bindings such as what we discuss here in the case of ListViews and TreeViews.


Basically TreeViews are widely used wherever user does not have a comprehensive map of the data structure inside the code in mind or when he/she decides to jump over nodes (e.g. drives and folders) by only one click. On the other hand ListViews – in form of what we have in Explorer - are suitable for users who just got used to the data structure. This supports this claim that TreeViews are suitable for data management and exploration but ListViews are better for applying data in program conventions. The first one acts in X/Y direction but the last one acts through Z.

Although the SubItems property of ListViews can improve the extensibility of ListViews, this property is not inherited from ListViewItem so it can not be converted to an identical data type to develop a tree shaped flow of data.

The key to connect these two controls is to save the current TreeNode in the ListView control.

Using the Code

The code is extremely simple. First of all we have to declare a ListView inherited class where we define our custom ListView.

Public Class BindedListView
Inherits ListView
End Class

Now a TreeView object should be declared in the ListView inherited class

Private _TreeView As TreeView 
Public Property TreeView() As TreeView 
Return _TreeView 
End Get 
Set(ByVal value As TreeView) 
If value IsNot Nothing Then 
_TreeView = value 
End If 
End Set 
End Property 

This TreeView is the main reference for our ListView treatments. Obviously, the nodes in the TreeView can not be replaced by TreeView because they have different types so another object should be declared. The _treenode object is the first node in the TreeView that represents the root node of the TreeView.

Private _treenode As TreeNode 

The initialize subroutine sets initial values as items.

Private Sub Initialize() 
_treenode = _TreeView.Nodes(0) 
SmallImageList = _TreeView.ImageList 
LargeImageList = _TreeView.ImageList 
For i As Integer = 0 To _treenode.Nodes.Count - 1 
End Sub 

Here the OnMouseClick event of the ListView has been overridden to determine whether user has clicked a TreeNode item or the first item which changes the current path to the upper path.

Protected Overrides Sub OnMouseDoubleClick(ByVal e As System.Windows.Forms.MouseEventArgs) 
If SelectedIndices.Count = 1 Then 
If SelectedIndices(0) > 0 Then 
End If 
End If 
End Sub 

In GoIn and GoOut subroutines the new TreeNode is being detected and its ChildNodes are being casted on the ListView. Finally, the old _treenode is replaced by new _treenode and a PathChanged event rises.

Sub GoIn() 
Dim inner As Integer = SelectedIndices(0) - 1 
If _treenode.Nodes(inner).Nodes.Count = 0 Then Exit Sub 
For i As Integer = 0 To _treenode.Nodes(inner).Nodes.Count - 1 
Items.Add(_treenode.Nodes(inner).Nodes(i).Text, _treenode.Nodes(inner).Nodes(i).ImageIndex) 
_treenode = _treenode.Nodes(inner) 
Catch ex As Exception 
End Try 
RaiseEvent PathChanged(_treenode.FullPath) 
End Sub 

Sub GoOut() 
If _treenode.Parent.Nodes.Count = 0 Then Exit Sub 
For i As Integer = 0 To _treenode.Parent.Nodes.Count - 1 
Items.Add(_treenode.Parent.Nodes(i).Text, _treenode.Parent.Nodes(i).ImageIndex) 
_treenode = _treenode.Parent 
Text = _treenode.FullPath 
Catch ex As Exception 
End Try 
RaiseEvent PathChanged(_treenode.FullPath) 
End Sub 


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


About the Author

Amir Emamjomeh
United Kingdom United Kingdom
Amir Emamjomeh is a senior petroleum reservoir engineer and also programmer who has been involved in several industrial and research projects proposed by the petroleum industry of Iran since 2006. His programming experiences include C, C++, C# , VB.Net, MATLAB and Visual FORTRAN.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Manoj Kumar Choubey28-Feb-12 19:24
professionalManoj Kumar Choubey28-Feb-12 19:24 
GeneralMy vote of 1 Pin
Drew Burchett1-Jul-11 3:22
memberDrew Burchett1-Jul-11 3:22 
GeneralRe: My vote of 1 Pin
Amir Emamjomeh1-Jul-11 22:13
professionalAmir Emamjomeh1-Jul-11 22:13 
GeneralMy vote of 1 Pin
dmex12-Jan-11 21:29
memberdmex12-Jan-11 21:29 
GeneralRe: My vote of 1 Pin
Drew Burchett1-Jul-11 3:18
memberDrew Burchett1-Jul-11 3:18 

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 | Cookies | Terms of Use | Mobile
Web03 | 2.8.190419.4 | Last Updated 4 Oct 2008
Article Copyright 2008 by Amir Emamjomeh
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid