WinForms TreeView
WinForms TreeView
GrapeCity
201 South Highland Avenue, Suite 301
Pittsburgh, PA 15206
Tel: 1.800.858.2739 | 412.681.4343
Fax: 412.681.4384
Website: https://www.grapecity.com/en/
E-mail: us.sales@grapecity.com
Trademarks
The ComponentOne product name is a trademark and ComponentOne is a registered trademark of GrapeCity, Inc. All
other trademarks used herein are the properties of their respective owners.
Warranty
ComponentOne warrants that the media on which the software is delivered is free from defects in material and
workmanship, assuming normal use, for a period of 90 days from the date of purchase. If a defect occurs during this
time, you may return the defective media to ComponentOne, along with a dated proof of purchase, and
ComponentOne will replace it at no charge. After 90 days, you can obtain a replacement for the defective media by
sending it and a check for $2 5 (to cover postage and handling) to ComponentOne.
Except for the express warranty of the original media on which the software is delivered is set forth here,
ComponentOne makes no other warranties, express or implied. Every attempt has been made to ensure that the
information contained in this manual is correct as of the time it was written. ComponentOne is not responsible for any
errors or omissions. ComponentOne’s liability is limited to the amount you paid for the product. ComponentOne is
not liable for any special, consequential, or other damages for any reason.
While you are welcome to make backup copies of the software for your own use and protection, you are not
permitted to make copies for the use of anyone else. We put a lot of time and effort into creating this product, and we
appreciate your support in seeing that it is used by licensed users only.
TreeView for WinForms 1
Table of Contents
Overview 3
Getting Started with WinForms Edition 3
Key Features 4
Quick Start 5
Step 1: Adding TreeView to the Application 5
Step 2: Creating Nodes in TreeView 5-8
Step 3: Running the Application 8
Design-Time Support 9
Collection Editors 9-11
Structure and Elements 12
Columns 13
Nodes 14
Adding and Removing Nodes 14-17
Reordering Nodes 17-18
Selecting Nodes 18-20
Navigating Nodes 20
Using Check boxes 20-22
Editing Nodes 22-23
Expanding and Collapsing Nodes 23-25
Dragging and Dropping Nodes 25-26
Overview
ComponentOne Studio introduces TreeView for WinForms, a control to help users display a hierarchical list of items
such as indexed entries, directories on a disk, headings in a document, etc. The TreeView control manages data
through nodes that can be selected, edited, and used to display simple text, images, and other elements such as check
boxes.
For more information on TreeView control, refer to the following links:
Key Features
Quick Start
Design-Time Support
Structure and Elements
Columns
Nodes
Data Binding
Styling and Appearance
Import and Export XML
API Reference
Key Features
TreeView for WinForms consists of the following key features:
Node presentation
Node editing
You can edit a node simply by setting the AllowEditing property to True and pressing F2 after selecting the
node. You can also cancel editing by pressing the Escape key.
Unbound mode
You can manually create a tree of nodes at design-time as well as run-time, thereby representing any data in a
hierarchical form.
Bound mode
You can bind TreeView for WinForms to multiple tables with hierarchical relations to display hierarchical data.
Also, you can bind the control to a self referencing data like a single list instead of multiple related lists.
Themes support
TreeView supports both keyboard and mouse navigation of nodes. In addition, the control supports single and
multiple node selection. You can select a node at both design-time and run-time. And you can select multiple
nodes in a contiguous or a non-contiguous manner.
Design-time support
TreeView lets you access collection editors right from the design form using Smart tag and context menu. The
collection editors allow you to add, remove nodes and columns without writing code.
Node expansion
You can expand either a single node or all nodes in the tree as per the requirement. You can also fully expand
the tree in one go using the ExpandAll method at the TreeView level. And you can even prevent end-users
from collapsing the expanded tree by cancelling the Collapsing event.
Quick Start
This quick start illustrates how to create a simple TreeView application either by using designer or through
programming in .NET.
Perform the following steps to walk through TreeView for WinForms quickly:
Using Designer
Through Code
Visual Basic
' add child node to parent node
node1.Nodes.Add(node2)
Design-Time Support
TreeView for WinForms provides design-time support that allows you to access collection editors right from the
form. To know more about collection editors, refer to Collection Editors.
Smart Tag
The smart tag for TreeView enables you to quickly access common C1TreeView Tasks involved with building
TreeView elements.
Edit nodes: Allows you to access TreeNodes Editor in just one click. For more information on TreeNodes
Editor, refer to Collection Editors.
Edit columns: Allows you to access C1TreeColumn Collection Editor in a single click. For more information
on C1TreeColumn Collection Editor, refer to Collection Editors.
About C1.Win.TreeView: Displays a dialog box that is helpful to find the version number.
Dock in Parent Container: Docks TreeView into the parent container.
Context Menu
You can access the context menu by right-clicking on the TreeView control at design-time. The context menu, just like
smart tag, provides shortcuts to access collection editors right from the design form.
Collection Editors
TreeView for WinForms includes two major collection editors- TreeNodes Editor and C1TreeColumn Collection Editor.
TreeNodes Editor enables you to add, remove parent nodes and child nodes in or from the Nodes collection of C1TreeView
and C1TreeNode easily. Using TreeNodes Editor allows you to not only modify the behavior of the nodes, but also reorder
them as per the requirement.
Following are the ways to access TreeNodes Editor:
Using Smart Tag and Context Menu
Refer to Design-Time Support for more information.
Through the Properties Window
Perform the following steps to access TreeNodes Editor through the Properties window:
C1TreeColumn Collection Editor allows you to add and remove columns in or from TreeView seamlessly. The editor lets you
customize the appearance and modify the behavior and data for the added columns.
The editor can be accessed in the following ways:
Using Smart Tag and Context Menu
Refer to Design-Time Support for more information.
Through the Properties Window
Columns
There are two types of columns in the TreeView control, bound and unbound. In the unbound mode, a column is not
bound to the fields of the data source and displays the string representation of the node. In the bound mode, a
column obtains its data from the TreeView's data source and displays the underlying data.
TreeView contains a single column by default. However, you can add multiple columns to the Columns collection of
TreeView to use the control in different scenarios. For instance, you may want to present sales data of different
regions for two consecutive years by using multiple columns.
The control allows you to add columns easily at design-time with C1TreeColumn Collection Editor. Refer to Collection
Editors for more information.
To create columns through code, you need to create an instance of C1TreeColumn and add it to the Columns
collection of C1TreeView. To remove all columns from the Columns collection of TreeView, you can use the Clear
method of C1TreeColumnCollection. Also, you can remove the first occurrence of a specified column from the
collection by using the Remove method that accepts the C1TreeColumn type instance as a parameter. And you can
remove a column at the specified index by using the RemoveAt method that accepts the column index value of the
Integer type as a parameter.
The following code snippet creates a column, specifies its header and name, and adds the column to the Columns
collection of TreeView.
Visual Basic
C#
// create a column
C1.Win.TreeView.C1TreeColumn Column1 = new C1.Win.TreeView.C1TreeColumn();
Nodes
In TreeView, nodes are arranged in a hierarchical form. Internally, the C1TreeNode class represents a node, provides
access to its data, and allows many operations to be performed on it. A few of them include adding, removing,
reordering, editing, navigating, and selecting nodes. Representing data in a tree-like structure, the TreeView control
stores nodes as nested collections. The collection of nodes can be accessed through the Nodes property
of C1TreeView. In the collection, each node can have its own child nodes accessible through the Nodes property
of C1TreeNode.
The following topics explain different ways of working with nodes in TreeView for WinForms:
The C1TreeNodeCollection class provides the Add method and the Insert method for adding nodes in TreeView. To
add a parent node, you can use either of the two methods with the Nodes collection of the C1TreeView class. Next, to
add a child node in the parent node, or children nodes in a child node, you can either use the Add method or
the Insert method with the Nodes collection of the C1TreeNode class.
The following code snippet demonstrates how to add nodes in the TreeView control using the Add method.
Visual Basic
' add the child nodes to the parent nodes' Nodes collection
parentNode1.Nodes.Add(childNode1)
C#
The following code snippet demonstrates how to add nodes in the TreeView control using the Insert method.
Visual Basic
' insert the third parent node in the TreeView Nodes collection
C1TreeView1.Nodes.Insert(2, parentNode3)
' insert the third child node in the third parent node's Nodes collection
parentNode3.Nodes.Insert(0, childNode3)
C#
// insert the third child node in the third parent node's Nodes collection
parentNode3.Nodes.Insert(0, childNode3);
Removing Nodes
The C1TreeNodeCollection class provides the Remove method and the RemoveAt method for removing nodes from
TreeView. You can use either of the two methods with the Nodes collection of C1TreeView to remove parent nodes
from TreeView. To remove a child node from a parent node or children nodes from a child node, you can use either of
the two methods with the Nodes collection of C1TreeNode.
The Remove method and the RemoveAt method removes a single node at a time. However, to remove all nodes
simultaneously from the Nodes collection of TreeView, a parent node, or a child node, you can use the Clear method
with the Nodes collection of C1TreeView or C1TreeNode, respectively.
Here is the code snippet demonstrating how to set these methods.
Visual Basic
' remove the second parent node from the TreeView Nodes collection
C1TreeView1.Nodes.Remove(parentNode2)
C#
// remove the second parent node from the TreeView Nodes collection
c1TreeView1.Nodes.Remove(parentNode2);
After removing the nodes, the treeview appears like the following.
Reordering Nodes
In TreeView, you can programmatically reorder nodes using the Move method of the C1TreeNodeCollection class.
The Move method can be used with the Nodes collection of either C1TreeView or C1TreeNode. It allows you to move
a node from its old position to a new position. The method accepts two parameters, oldIndex and newIndex of the
Integer type. Here oldIndex and newIndex specify the old and the new position of the node respectively. The Move
method, however, cannot be used to move a child node from one parent node to another.
The following code snippets moves Child1 from index 0 to index 2 in Parent1.
Visual Basic
C#
Selecting Nodes
To select TreeView nodes in different ways, you need to set the SelectionMode property of C1TreeView. The
SelectionMode property accepts the following values from the C1TreeViewSelectionMode enumeration:
Multi: Allows you to select multiple nodes (parent or child) in TreeView. You can select multiple nodes either in
a contiguous or a non-contiguous manner. To select multiple nodes in a contiguous manner, hold the SHIFT
key while clicking on nodes. And to select nodes in a non-contiguous manner, hold the CTRL key while clicking
on nodes.
MultiSibling: Allows you to select multiple nodes in a contiguous or a non-contiguous manner using SHIFT or
CTRL keys respectively, within a parent node only.
None: Does not allow you to select any node in TreeView.
Single (default value): Allows you to select only one node (parent or child) in TreeView at a time.
Below is the sample code snippet setting the SelectionMode property.
Visual Basic
C#
Contiguous Selection
Non-contiguous Selection
You can also select a node by setting the Selected property of C1TreeNode to True.
The Selected property of C1TreeNode is not applicable when the SelectedMode property of C1Treeview is set
to None.
Visual Basic
' enable the Selected property for the first parent node
C1TreeView1.Nodes(0).Selected = True
C#
The node for which the property is set gets selected as shown below.
Navigating Nodes
TreeView supports both mouse and keyboard navigation of nodes.
Mouse Navigation
The following table lists the actions and the corresponding Mouse commands for navigating nodes in TreeView:
Collapse a node Click on the minus sign at the left of the node's name.
Keyboard Navigation
The following table describes the actions and their associated keys to use when navigating nodes in TreeView:
Select multiple nodes MOUSE + CTRL (distributed selection) / MOUSE + SHIFT (Continuous selection)
Visual Basic
C#
The check box next to a node can be checked by setting the Checked property of C1TreeNode to True. You can also
alter the state of the check box of a node by setting the CheckState property of C1TreeNode to any of the following
values from the CheckState enumeration of System.Windows.Forms: Checked, Indeterminate, and Unchecked.
In addition, you can change the state of the check box of a node by using the Check or the Uncheck method
of C1TreeNode, respectively. These methods accept a parameter allChildrens of the Boolean type. The parameter
allChildrens determines whether the check boxes of all child nodes within a node will be checked or
unchecked. C1TreeView provides the CheckAll or the UncheckAll method using which you can check or uncheck all
the check boxes beside the nodes. Moreover, using these methods, you can check nodes recursively in which if a
parent node is checked, all the child nodes under it are recursively checked.
See the following code snippet for reference.
Visual Basic
C#
Editing Nodes
TreeView enables you to edit nodes in applications, you need to set the AllowEditing property of the C1TreeView
class to true. The default value of the property is false.
You can start editing a node by selecting a node and pressing the Enter or F2 key, or simply double-clicking the node
itself. In addition, you can edit a node programmatically by calling the BeginEdit method in code. To use any of these
modes for editing nodes, you need to set the EditMode property of the C1TreeView class from the
C1TreeViewEditMode enum.
Note: If you want to determine whether the contents of a specific node can be changed, you can use
the IsReadOnly method of C1TreeNode.
Visual Basic
C#
The C1TreeNode class provides the Expand method to expand a single node (parent or child). The Expand method
accepts Boolean values to determine whether the child nodes within a particular node should expand or not. On setting this
method to true, all the child nodes along with the selected node gets expanded. If set to false, only the node at which the
method is called gets expanded. The following code snippet demonstrates how to expand a single node using the Expand
method.
Visual Basic
' expand the first parent node without expanding its child nodes
parentNode1.Expand(False)
C#
// expand the first parent node without expanding its child nodes
parentNode1.Expand(false);
The parent node gets expanded without expanding the child nodes, as shown below.
The C1TreeNode class also provides the Expanded property to expand a single node at a time.
Additionally, the C1TreeView class provides the ExpandAll method to fully expand a TreeView. In fully expanded state, the
TreeView displays all the children nodes under a parent node as shown in the image below.
The following code snippet demonstrates how to fully expand a TreeView using the ExpandAll method.
Visual Basic
C#
Once the TreeView is in fully expanded state, you can prevent it from collapsing. This can be achieved by cancelling
the Collapsing event of the C1TreeView class as demonstrated in the code below.
Visual Basic
C#
Collapsing Nodes
Visual Basic
C#
While Dragging
After Dropping
Custom Editors
TreeView, by default, uses a textbox editor for editing nodes. You can, however, replace the default editor with a
custom editor, whenever needed. You can specify a custom editor for nodes for each column by using the Editor
property of C1TreeColumn. In addition, you can set the type of the node editor by setting the EditorType property
from the C1TreeViewEditorType enum.
The following image displays both the default textbox and the custom textbox editor.
The following code snippet creates a class EditorsData that provides sample data for the editors.
Visual Basic
Imports System.ComponentModel
Public Class EditorsData
Public Property TextBoxValue() As Object
Get
Return m_TextBoxValue
End Get
Set
m_TextBoxValue = Value
Return data
End Function
End Class
C#
using System.ComponentModel;
namespace SamplesData
{
public class EditorsData
{
public object TextBoxValue
{
get; set;
}
public EditorsData()
{
Collection = new BindingList<EditorsData>();
}
return data;
}
}
}
The following code snippet creates a class CustomTextBox to create a custom textbox editor on the basis of the
default textbox editor.
Visual Basic
Imports C1.Win.TreeView
If attrs.Contains("AcceptReturn") Then
AcceptsReturn = CBool(attrs("AcceptReturn"))
End If
If attrs.Contains("AcceptTab") Then
AcceptsTab = CBool(attrs("AcceptTab"))
End If
If attrs.Contains("BackColor") Then
BackColor = DirectCast(attrs("BackColor"), Color)
End If
If attrs.Contains("Font") Then
Font = DirectCast(attrs("Font"), Font)
End If
If attrs.Contains("ForeColor") Then
ForeColor = DirectCast(attrs("ForeColor"), Color)
End If
If attrs.Contains("DisabledForeColor") Then
ForeColor = DirectCast(attrs("DisabledForeColor"), Color)
End If
If attrs.Contains("MaxLength") Then
MaxLength = CInt(attrs("MaxLength"))
End If
If attrs.Contains("ReadOnly") Then
[ReadOnly] = CBool(attrs("ReadOnly"))
End If
If attrs.Contains("WordWrap") Then
WordWrap = CBool(attrs("WordWrap"))
End If
Text = value.ToString()
End Sub
C#
using C1.Win.TreeView;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;
namespace CustomEditors
{
public class CustomTextBox : TextBox, IC1TreeEditor
{
public object C1EditorGetValue()
{
return Text;
}
Text = value.ToString();
}
The following code snippet sets the default textbox and the custom textbox as editors in the first and the second
columns, respectively.
Visual Basic
C1TreeView1.Columns.Clear()
C1TreeView1.Columns.Add(column1)
C1TreeView1.Columns.Add(column2)
column1.Name = "clnTextBox"
column1.HeaderText = "TextBox"
column2.Name = "cnlCustomTextBox"
column2.HeaderText = "Custom TextBox"
column2.Width = 90
column2.DisplayFieldName = "TextBoxValue\TextBoxValue"
column2.Editor = New CustomTextBox()
column2.EditorType = C1.Win.TreeView.C1TreeViewEditorType.Text
C1TreeView1.DataMember = "Collection\Collection"
C1TreeView1.DataSource = EditorsData.GetData()
C#
c1TreeView1.Columns.Add(column1);
c1TreeView1.Columns.Add(column2);
column1.Name = "clnTextBox";
column1.HeaderText = "TextBox";
column1.Width = 60;
column1.DisplayFieldName = "TextBoxValue\\TextBoxValue";
column1.Editor = textBox1;
column1.EditorType = C1.Win.TreeView.C1TreeViewEditorType.Text;
column2.Name = "cnlCustomTextBox";
column2.HeaderText = "Custom TextBox";
column2.Width = 90;
column2.DisplayFieldName = "TextBoxValue\\TextBoxValue";
column2.Editor = new CustomTextBox();
column2.EditorType = C1.Win.TreeView.C1TreeViewEditorType.Text;
c1TreeView1.DataMember = "Collection\\Collection";
c1TreeView1.DataSource = EditorsData.GetData();
Custom Nodes
TreeView allows creating and displaying custom nodes in place of the default nodes. It is possible to add custom
properties and show various customizations, for instance, including, scaling, and aligning images, displaying borders,
customizing font styles and colors, and changing dimensions etc. To set custom nodes in TreeView, set
the CustomContentPresenter property of C1TreeColumn.
The following image displays custom nodes in TreeView.
To create custom nodes in TreeView, create custom classes to define properties and methods that apply to custom
nodes and override the default ones. After creating custom classes, initialize their instances and set custom nodes
using the CustomContentPresenter property.
The following code snippets show how to create custom classes to define main elements for custom nodes.
Visual Basic
_gp.Rows.Add()
' ReorderLevel
_gp(0, 0).Element = New TextElement(lStyle, "Reorder level:")
_gp(0, 1).Element = _reorderLevel
_gp.Rows.Add()
' UnitPrice
_gp(1, 0).Element = New TextElement(lStyle, "Unit price:")
_gp(1, 1).Element = _unitPrice
_gp.Rows.Add()
' QuantityPerUnit
_gp(2, 0).Element = New TextElement(lStyle, "Quantity per unit:")
_gp(2, 1).Element = _quantityPerUnit
_gp.Columns.Add()
' labels
_gp.Columns(2).Width = 100
_gp.Columns.Add()
C#
public ProductCustomNode()
{
// level 0
// count in category
_count = new TextElement();
_count.Style = new Style();
_count.Style.Margins = new Thickness(2);
// level 1
// lable style
var lStyle = new Style();
lStyle.HorizontalAlignment = Alignment.Far;
lStyle.Margins = new Thickness(1);
lStyle.Font = new Font("Calibri", 9, FontStyle.Regular);
// elements style
_eStyle = new Style();
_eStyle.Margins = new Thickness(1);
_eStyle.Font = new Font("Calibri", 9, FontStyle.Regular);
// init elements
_quantityPerUnit = new TextElement(_eStyle.Clone());
_unitPrice = new TextElement(_eStyle.Clone());
_unitInStock = new TextElement(_eStyle.Clone());
_unitsOnOrder = new TextElement(_eStyle.Clone());
_reorderLevel = new TextElement(_eStyle.Clone());
// init a grid for text elements
_gp = new GridPanel();
_gp.Columns.Add(); // labels
_gp.Columns[0].Width = 100;
_gp.Rows.Add(); // ReorderLevel
_gp[0, 0].Element = new TextElement(lStyle, "Reorder level:");
_gp[0, 1].Element = _reorderLevel;
_gp.Rows.Add(); // UnitPrice
_gp[1, 0].Element = new TextElement(lStyle, "Unit price:");
_gp[1, 1].Element = _unitPrice;
_gp.Rows.Add(); // QuantityPerUnit
_gp[2, 0].Element = new TextElement(lStyle, "Quantity per unit:");
_gp[2, 1].Element = _quantityPerUnit;
_gp.Columns.Add(); // labels
_gp.Columns[2].Width = 100;
_gp.Columns.Add(); // text
// UnitsInStock
_gp[0, 2].Element = new TextElement(lStyle, "Units in stock:");
_gp[0, 3].Element = _unitInStock;
// UnitsOnOrder
_gp[1, 2].Element = new TextElement(lStyle, "Units on order:");
_gp[1, 3].Element = _unitsOnOrder;
The following code snippet shows how to set custom nodes using the CustomContentPresenter property.
Visual Basic
C#
Searching Nodes
TreeView enables you to search for the nodes matching the specified string. To search for a node in TreeView, you
need to use Search method of C1TreeView class. The Search method takes string as a value, searches for the nodes
using depth-first search and returns the node containing the searched string.
The following image shows the searched node containing “Chai” as input string.
To search for the nodes containing the string you input, use the following code. In this example, we have taken a text
box where you can provide a string to be searched on a button click.
Data Binding
The process of connecting a data consumer with a data source is known as data binding. TreeView for WinForms
supports data binding in two modes: unbound mode and bound mode.
The unbound mode means that the TreeView control is not bound to any data source. In the unbound mode, you
need to create columns and nodes manually either by using designer or programmatically. To create nodes in the
unbound mode at design-time, you can simply add the nodes in TreeNodes Editor. For more details on how
to access TreeNodes Editor, refer to Collection Editors. If you want to create nodes in the unbound mode
programmatically, you can do so by using the Add and the Insert methods. For more information, refer to Adding and
Removing Nodes.
In the bound mode, the TreeView control uses data from a data source and displays it in the form of parent and child
nodes in a hierarchical manner. TreeView supports a number of data source objects, namely, BindingSource, List,
DataView, DataTable, DataSet, and objects implementing particular interfaces like IBindingList and IList.
To bind TreeView to a data source object, you need to use the DataSource property of C1TreeView. You can specify
the type of the bound mode for TreeView by setting the TreeBoundMode property to one of the following values
from the TreeBoundMode enumeration:
OneWay: Does not update the data source after the treeview hierarchy changes.
TwoWay: Updates the data source after the treeview hierarchy changes
Once you have set the data source, you can set the list of fields to display in columns by using the DisplayFieldName
property of C1TreeColumn.
Refer to the following sections to know how you can bind TreeView to different objects:
Visual Basic
C#
// create a class
public class Products
{
Visual Basic
C#
Visual Basic
C#
// create a class
public class Product
{
public Guid ID { get; set; }
public string Name { get; set; }
public double Price { get; set; }
Visual Basic
C#
// create a class
public class ProductsGroup
{
public Guid ID { get; set; }
public string Name { get; set; }
public BindingList<Product> Products { get; set; }
public int CountOfProducts
{
get { return Products.Count; }
}
Visual Basic
C#
// create a class
public class Store
{
public Guid ID { get; set; }
public string Name { get; set; }
public BindingList<ProductsGroup> ProductsGroups { get; set; }
public int CountOfProducts
{
get
{
int count = 0;
foreach (var pg in ProductsGroups)
count += pg.CountOfProducts;
return count;
}
}
Visual Basic
C#
// create a class
public class StoreCollection : BindingList<Store>
{
public static StoreCollection GetData()
{
var stores = new StoreCollection();
return stores;
}
}
The below-mentioned code snippet binds the TreeView control to these classes.
Visual Basic
C1TreeView1.Columns.Clear()
C1TreeView1.DataSource = Nothing
C1TreeView1.DataMember = "\ProductsGroups\Products"
Dim column = New C1.Win.TreeView.C1TreeColumn()
column.HeaderText = "Name"
C1TreeView1.Columns.Add(column)
column = New C1.Win.TreeView.C1TreeColumn()
column.DisplayFieldName = "CountOfProducts\CountOfProducts\"
column.HeaderText = "Products in store"
C1TreeView1.Columns.Add(column)
column = New C1.Win.TreeView.C1TreeColumn()
column.DisplayFieldName = "\\Price"
column.HeaderText = "Price"
C1TreeView1.Columns.Add(column)
C1TreeView1.DataSource = StoreCollection.GetData()
End Sub
C#
public Form1()
{
InitializeComponent();
c1TreeView1.Columns.Clear();
c1TreeView1.DataSource = null;
c1TreeView1.DataMember = "\\ProductsGroups\\Products";
var column = new C1.Win.TreeView.C1TreeColumn();
column.HeaderText = "Name";
c1TreeView1.Columns.Add(column);
column = new C1.Win.TreeView.C1TreeColumn();
column.DisplayFieldName = "CountOfProducts\\CountOfProducts\\";
column.HeaderText = "Products in store";
c1TreeView1.Columns.Add(column);
column = new C1.Win.TreeView.C1TreeColumn();
column.DisplayFieldName = "\\\\Price";
column.HeaderText = "Price";
c1TreeView1.Columns.Add(column);
c1TreeView1.DataSource = StoreCollection.GetData();
}
Themes
Tree Lines
Right-to-Left Support
Custom Button Images for Nodes
Icons for Expand Button and Checkbox
Themes
You can apply a number of built-in or custom themes to present TreeView with a consistent and customized look by
using Themes for WinForms. For more information, refer to the Themes for WinForms documentation.
Tree Lines
Tree lines, as the name suggests, are the lines connecting the nodes of a treeview displaying the relationship between
parent and child nodes clearly.
TreeView allows you to display or hide the tree lines by setting the ShowLines property provided by
the TreeViewStyles class to true or false. In addition, the control enables you to perform customizations, as follows:
Color: Change the color of the connected lines between the nodes by setting the LinesColor property of
the TreeViewStyles class.
Width: Set the width of the tree lines by setting the LinesWidth property of TreeViewStyles to integer values.
Style: Specify the style of the tree lines by setting the LinesStyle property of TreeViewStyles from the DashStyle
enumeration.
The following image shows TreeView with the tree lines customized with respect to their color, width, and style.
The following code snippet enables the visibility of the tree lines and customizes them in terms of their width, color,
and style.
Visual Basic
C#
Right-to-Left Support
TreeView provides support for languages that follow Right-to-Left scripts. The control enables you to present all
treeview contents in the right-to-left direction by using the RightToLeft property of
System.Windows.Forms.Control. You can set the property to any of the values in the RightToLeft enumeration of
Systems.Windows.Forms as follows:
No: Sets the direction from left to right, which is the default direction.
Yes: Sets the direction from right to left.
Inherit: Inherits the direction from the parent control.
The following code snippets illustrate how to set these properties in code.
Visual Basic
C#
Visual Basic
' set the image list instance as the TreeView image list
C1TreeView1.ImageList = imageList
C#
Visual Basic
C#
Visual Basic
C#
Visual Basic
C#
The ExpressionInfo class provides various other properties listed below that can be used to manipulate expressions in
TreeView control. These properties can be used to add, edit, or delete expressions on any of the node levels available
in TreeView.
Property Description
AllowAddNew Indicates the ability to add node level expression
AllowEdit Indicates whether the expressions of this column can be edited by clicking the Expression
Editor icon in the column header
ExpandLastExpression Indicates whether the last expression expand to all subsequent levels of the TreeView even if
the expression is not defined for them
The following code demonstrates integration of TreeView with Expression Editor. In this code, expression is used on
the fifth column of the TreeView control which contains two levels. Here, we have added an expression for the second
level only.For this, we set our expression to the second item of the Expressions array, and set the first item as empty.
C#
private void Form1_Load(object sender, EventArgs e)
{
this.categoriesTableAdapter.Fill(this.c1NWindDataSet.Categories);
this.productsTableAdapter.Fill(this.c1NWindDataSet.Products);
c1TreeView1.Columns[4].ExpressionInfo.Expressions = new
string[] { "", "[UnitPrice]*([UnitsInStock]+[UnitsOnOrder])" };