C# TreeListView Control by 10Tec

by Igor Katenov, the lead developer at 10Tec

"C# treelistview" is a search query frequently used by developers when they are searching for a C# control that combines both TreeView and ListView functionality. In other words, a TreeListView control is a ListView in report mode that displays a hierarchical view of information.

10Tec iGrid.NET for WinForms is a good candidate to consider if you need a TreeListView control for C# or VB.NET application:

C# TreeListView control in action

Below in this article we will briefly describe the main advantages of iGrid used as a VB.NET/C# TreeListView control in Windows Forms.NET.

Free C# TreeListView solutions

You can easily find a series of free C# TreeListView controls with full source code in the Internet. Perhaps, the oldest of them is TreeListView developed by Thomas Caudal and published on CodeProject in 2002. Another famous and open-source C# TreeListView control is ObjectListView hosted on SourceForge. Both controls inherit the .NET ListView control and add the TreeView functionality to it. Let’s look a little bit more careful at such solutions by the example of ObjectListView, which is based on an enough interesting ideology.

This C# TreeListView control allows the developer to implement many tasks easily - even using something as declarative programming instead of coding. When you work with this control, you do not add items manually one-by-one. Instead, you simply feed a collection of objects to the SetObjects method, and the control does the rest automatically. Other traditional operations performed with table data as sorting and grouping are also implemented automatically or can be adjusted.

What we are interested in this control is its treelistview functionality. ObjectListView.dll is a control library, and the requested functionality is provided by the TreeListView control from it (which is actually a descendant of ObjectListView):

Getting started with C# TreeListView control

To create this hierarchical representation of items in this TreeListView control, you just specify the collection of root objects and write a couple of methods used to get the list of child nodes dynamically. Very easy and intuitive!

Unfortunately, ObjectListView inherits all drawbacks of the WinForms ListView control. The main of them are flickering and poor performance when working with large sets of data. The bad performance and drawing problems becomes noticeable even for collection of several hundred items when we use the TreeListView control. You can see this even in the demo provided by the author, and this problem is actively discussed on the project’s forum.

Other points worth attention if we talk about TreeListViews could be:

  • You cannot select subitems as cells in a grid control (only the primary item or full row can be selected).
  • To edit values from non-primary columns, you can use only the mouse – the keyboard interface is not available for them.
  • The user cannot sort the TreeListView control by several columns.
  • The tree column or other columns cannot be fixed (frozen) so they do not scroll.

There are other similar solutions you can find on CodeProject or other great source code resources, but it seems all those controls are based on the standard TreeView or ListView controls. If so, they all will have similar problems we have listed above.

iGrid.NET as a TreeListView control

iGrid.NET was designed as a grid control first of all, but the ability to use it as a VB.NET or C# TreeListView control is its built-in feature. The developer can easily turn any column of iGrid into a tree column with just one property, and this column can be really placed at any position in the grid:

10Tec C# TreeListView control

Another key characteristic of the iGrid control that we are constantly focusing on is its excellent performance for big amounts of data – even when it works in the TreeListView control mode. For iGrid, it is not a problem to accommodate 100’000 or even 1’000’000 rows of tabular data and still provide your users with a responsible and flicker-free tool to browse them. This is done due to the efficient internal memory manager and drawing code, which were developed specially for cases like this.

If we compare iGrid to the C# TreeListView controls discussed in the previous section of this article, we can highlight the following benefits of this control:

  • Every bit of data (the primary item or its subitems) can be selected and edited using the mouse or keyboard.
  • Multi-column sorting is available for the user (just click the column headers of required columns holding down a modifier key like CTRL).
  • Some first columns including the tree column can be frozen, so they will be always visible even if the user has scrolled the TreeListView control with a big number of columns in the horizontal direction.

The treelistview functionality in iGrid is based on the concept of row level, when an integer value associated with every row defines its hierarchy level. One little drawback of this concept is that you cannot easily get all child nodes of a row like you can do that in the WinForms TreeView using the Nodes property of the TreeNodeCollection type every node has – you need to write a simple loop that enumerates grid rows for that. However, the flip side of this is that you can populate the grid from a data source and create the tree representation of your data with just one call to the overloaded FillWithData method in which the corresponding parameter is used to specify the row level field!

To summarize, 10Tec iGrid.NET can be used as a powerful C# TreeListView control with excellent performance in real-world projects, when the features and/or performance provide by open-source C# TreeListView solutions are not enough to make end users happy.

Related article: TreeView in DataGridView »