BinaryTreeListView
A tree and a grid in one control: rows that nest, expand and collapse like a tree view, laid out
in sortable, resizable, editable columns like a DataGridView.
public partial class BinaryTreeListView : DataGridView
Inherits from / implements: DataGridView
Remarks
Use it wherever data has both a hierarchy and several facts per row: a project plan with
owners, due dates and progress, a file system with sizes and dates, a chart of accounts with
balances, a bill of materials with quantities and costs. It is a DataGridView
underneath, so fonts, cell styles, column sizing, selection and editing work as they do in any
grid.
Columns
Add the columns before any rows. Exactly one column should be a
BinaryTreeListViewColumn: that column carries the tree – the indent, the expand
glyph, the connecting lines and the node's image or check box. It can be at any position. The
other columns can be any DataGridViewColumn, including the editing columns made
for this control: BinaryTreeListViewDatePickerColumn,
BinaryTreeListViewNumericUpDownColumn, BinaryTreeListViewComboBoxColumn,
BinaryTreeListViewOffice2007ColorPickerColumn,
BinaryTreeListViewExtendedTextBoxColumn and
BinaryTreeListViewNumericExtendedTextBoxColumn.
Nodes
Every row is a BinaryTreeListViewNode. Build the tree through
Nodes, never through the grid's own rows: Nodes.Add(...) takes one value per
column, in column order, and returns the new node, whose own BinaryTreeListViewNode.Nodes
takes its children the same way. A node starts collapsed; call
BinaryTreeListViewNode.Expand() or ExpandAllNodes to open it. Keep your
own object with the row in Tag. To build the tree from a table
instead, set HierarchicalDataSource to a DataTable whose first three
columns are UID, ID and ParentID.
Expanding and collapsing
The user opens and closes a node by clicking its glyph, by double-clicking the tree cell
(ExpandCollapseNodeOnDoubleClick), and with Shift+Right and Shift+Left
(ExpandCollapsingOfNodesLevel). Each change raises
BinaryTreeListViewNodeExpanding or BinaryTreeListViewNodeCollapsing
first, which can cancel it or – for expanding – add the children at that moment, and then
BinaryTreeListViewNodeExpanded or BinaryTreeListViewNodeCollapsed.
Rows that are inside a collapsed node are not in the grid at all, so the grid's row indices count
only the rows on show.
Sorting
Turn on AllowUserToSortColumns and a click on a heading sorts by that column,
a second click reverses it and a third puts the original order back; SortNodesByColumn
does the same from code. Siblings are sorted within their parent at every level, so the tree keeps
its shape. The grid's own column sorting is not used: every column is made
NotSortable as it is added.
Editing
Cells are edited as in any grid, starting the way CellEditingMode says – set
that rather than DataGridView.EditMode, which the control switches as the user
clicks between the tree's glyph area and the text. F2 always starts an edit. The special editing
columns show their editor's chrome – a spinner, a drop-down button, a colour swatch – on every
row while SpecialColumnCellsToDisplayEditingControlOnCellsAlways is on, so the grid
reads as a form.
Appearance
The connecting lines are drawn in LineDrawingColor and
LineDrawingDashStyle, or not at all (DrawNodeConnectingLines). The
expand glyph is the system tree glyph unless both ExpandImage and
CollapseImage are set. Each node shows an image (NodeImage,
BinaryTreeListViewNode.Image), or a check box or radio button
(SetUpNodeControlType). UseBuiltInRendering paints the headings and
rows in gradients, and ShouldDrawFrame gives the control a rounded border with a
soft shadow.
Drag and drop
With EnableAutomaticBuiltInDragDrop on, the user can drag a node, with
everything beneath it, onto another node to make it that node's child, or onto the empty area
below the last row to make it a top-level node. For your own drag and drop, set
Control.AllowDrop and handle ItemDrag.
The control can also run as a plain grid with the same appearance features: set
ControlDataDisplayMode to ControlDataDisplayMode.ListView
before adding columns, and use ordinary columns and the grid's own DataSource.
Examples
A project plan on a form, built in code. The first column is the tree; the others show an
owner, a due date that edits with a calendar, and progress that edits with a spinner. Each node
keeps its WorkItem in Tag.
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using Binarymission.WinForms.Controls.TreeControls;
public sealed class WorkItem
{
public string Title { get; set; }
public string Owner { get; set; }
public DateTime Due { get; set; }
public decimal PercentDone { get; set; }
}
public sealed class ProjectPlanForm : Form
{
private readonly BinaryTreeListView plan = new BinaryTreeListView();
private readonly Label status = new Label();
public ProjectPlanForm()
{
Text = "Website relaunch";
ClientSize = new Size(820, 460);
plan.Dock = DockStyle.Fill;
plan.AllowUserToSortColumns = true;
plan.LineDrawingColor = Color.Silver;
plan.LineDrawingDashStyle = DashStyle.Dot;
plan.ShouldDrawFrame = true;
plan.FrameFocusedBorderColor = Color.SteelBlue;
// Columns first: the tree column, then one column per fact.
plan.Columns.Add(new BinaryTreeListViewColumn { Name = "Task", HeaderText = "Task", FillWeight = 260 });
plan.Columns.Add(new DataGridViewTextBoxColumn { Name = "Owner", HeaderText = "Owner" });
plan.Columns.Add(new BinaryTreeListViewDatePickerColumn { Name = "Due", HeaderText = "Due", Format = BinaryDatePickerFormat.Short });
plan.Columns.Add(new BinaryTreeListViewNumericUpDownColumn { Name = "Done", HeaderText = "Done %", Maximum = 100 });
BinaryTreeListViewNode design = AddTask(plan.Nodes, new WorkItem { Title = "Design", Owner = "Priya Shah", Due = new DateTime(2026, 11, 6), PercentDone = 60 });
AddTask(design.Nodes, new WorkItem { Title = "Wireframes", Owner = "Priya Shah", Due = new DateTime(2026, 10, 23), PercentDone = 100 });
AddTask(design.Nodes, new WorkItem { Title = "Visual design", Owner = "Tom Hughes", Due = new DateTime(2026, 11, 6), PercentDone = 20 });
BinaryTreeListViewNode build = AddTask(plan.Nodes, new WorkItem { Title = "Build", Owner = "Ana Costa", Due = new DateTime(2026, 12, 11), PercentDone = 10 });
AddTask(build.Nodes, new WorkItem { Title = "Checkout", Owner = "Ana Costa", Due = new DateTime(2026, 12, 4), PercentDone = 0 });
plan.ExpandAllNodes();
// Keep a summary task open while it still has work outstanding.
plan.BinaryTreeListViewNodeCollapsing += (sender, e) =>
{
e.Cancel = ((WorkItem)e.Node.Tag).PercentDone < 100;
};
plan.SelectionChanged += (sender, e) =>
{
BinaryTreeListViewNode node = plan.CurrentNode;
status.Text = node == null ? string.Empty : ((WorkItem)node.Tag).Title + " - " + ((WorkItem)node.Tag).Owner;
};
status.Dock = DockStyle.Bottom;
Controls.Add(plan);
Controls.Add(status);
}
private static BinaryTreeListViewNode AddTask(BinaryTreeListViewNodesCollection nodes, WorkItem item)
{
// One value per column, in column order.
BinaryTreeListViewNode node = nodes.Add(item.Title, item.Owner, item.Due, item.PercentDone);
node.Tag = item;
return node;
}
}See also
Members at a glance
| Constructors | |
|---|---|
BinaryTreeListView | Creates a tree list view with no columns and no nodes. |
| Properties | |
AllowDataErrorExceptionToBeThrown | Gets or sets whether a cell value that cannot be formatted or committed is reported as a
DataGridView normally reports it. |
AllowUserToAddRows | Gets or sets whether the grid shows the row for adding a new row at the bottom. |
AllowUserToDeleteRows | Gets or sets whether the user can delete rows with the Delete key. |
AllowUserToSortColumns | Gets or sets whether clicking a column heading sorts the tree by that column: ascending, then descending, then back to the order the nodes were built in. |
BuiltInCustomRenderedTargetElement | Gets or sets which parts of the grid the built-in renderer paints: the column headings, the row headings, the rows, or a combination. |
BusyIndicatorText | Gets or sets the message shown in the middle of the control while
HierarchicalDataSource is being bound. |
BusyIndicatorTextBackColor | Gets or sets the background colour of the message shown while HierarchicalDataSource
is being bound. |
BusyIndicatorTextFont | Gets or sets the font of the message shown while HierarchicalDataSource is being
bound. |
BusyIndicatorTextForeColor | Gets or sets the text colour of the message shown while HierarchicalDataSource is
being bound. |
CellBorderColor | Gets or sets the colour of the border round the current cell. |
CellBorderDrawingColor | Gets or sets the colour of the cell borders the built-in renderer draws on the rows. |
CellBorderWidth | Gets or sets the width of the border round the current cell. |
CellEditingMode | Gets or sets how the user starts editing a cell. Set this instead of the grid's
DataGridView.EditMode. |
CollapseImage | Gets or sets the image drawn as the collapse glyph of an open node. |
ColumnBackgroundDefaultCellStyleBackColor | Gets or sets the background colour a tinted column is given back when the pointer, or the current cell, leaves it. |
ColumnBackgroundOnCellSelectionColor | Gets or sets the colour the column of the current cell is tinted in. |
ColumnBackgroundOnMouseHoverColor | Gets or sets the colour the column under the pointer is tinted in. |
ColumnHeaderBottomBorderColor | Gets or sets the colour of a rule drawn along the bottom of the column headings, parting them from the rows. |
ColumnSeparatorDrawingColor | Gets or sets the colour of the line the built-in renderer draws between column headings. |
ControlDataDisplayMode | Gets or sets whether the control works as a tree list view or as a plain grid with this control's appearance features. |
CurrentNode | Gets the node of the current cell – the row the user is on. |
DepthForPreparingNodesWithChildren | Gets or sets how many passes binding makes over HierarchicalDataSource to place rows
whose parent comes after them in the table. |
DrawColumnSeparatorWhenUsingBuiltinRendering | Gets or sets whether the built-in renderer draws a line between column headings. |
DrawGlassBackgroundHighlightingOnMouseEnter | Gets or sets whether the row under the pointer is highlighted. |
DrawNodeConnectingLines | Gets or sets whether lines are drawn joining each node to its parent and its siblings. |
EnableAutomaticBuiltInDragDrop | Gets or sets whether the user can rearrange the tree by dragging nodes with the mouse. |
EnableCommitingEditWhenMovingFocusFromEditContext | Gets or sets whether an edit in progress is committed when the user clicks the glyph, check box or image area of a tree cell. |
EnableExpandCollapsingOfNodesUponMouseClickOnReadOnlyTreeListViewNodeCellOrColumn | Gets or sets whether, in a read-only tree, a click anywhere in a node's tree cell opens or closes the node. |
EndColorForRenderingColumnHeader | Gets or sets the colour at the bottom or right of the gradient the built-in renderer fills the column headings with. |
EndColorForRenderingRow | Gets or sets the colour at the bottom or right of the gradient the built-in renderer fills each cell of the rows with. |
EndColorForRenderingRowHeader | Gets or sets the colour at the bottom or right of the gradient the built-in renderer fills the row headings with. |
ExpandCollapseNodeOnDoubleClick | Gets or sets whether double-clicking a node's cell in the tree column opens or closes the node. |
ExpandCollapsingOfNodesLevel | Gets or sets where the focus must be for Shift+Right and Shift+Left to open and close a node. |
ExpandImage | Gets or sets the image drawn as the expand glyph of a closed node that has children. |
ExpandTargetDropNodeUponDragDrop | Gets or sets whether the node a dragged node is dropped on is opened, with everything beneath it, so that the moved node can be seen in its new place. |
FrameBorderColor | Gets or sets the colour of the frame's border. |
FrameBorderThickness | Gets or sets the thickness of the frame's border. |
FrameCornerRadius | Gets or sets the radius of the frame's rounded corners. |
FrameFocusedBorderColor | Gets or sets the colour of the frame's border while the focus is in the control – on the grid or on a cell's editor. |
FramePadding | Gets or sets the space between the frame's border and the grid inside it. |
FrameShadowColor | Gets or sets the colour of the frame's drop shadow. |
FrameShadowDepth | Gets or sets how far the frame's shadow spreads. |
FrameShadowOpacity | Gets or sets the strength of the frame's shadow where it is darkest, next to the border. |
GlassBrushBottomHalfEndColor | Gets or sets the colour the hover highlight ends with – at the bottom of the row, or at its right
when OrientationAspectForRenderingRow is horizontal. |
GlassBrushBottomHalfStartColor | Gets or sets a colour kept for compatibility; the hover highlight does not use it. |
GlassBrushLinearGradientFactor | Gets or sets a value kept for compatibility; the hover highlight does not use it. |
GlassBrushTopHalfEndColor | Gets or sets a colour kept for compatibility; the hover highlight does not use it. |
GlassBrushTopHalfStartColor | Gets or sets the colour the hover highlight starts with – at the top of the row, or at its left
when OrientationAspectForRenderingRow is horizontal. |
HierarchicalDataSource | Gets or sets a table to build the whole tree from, in place of adding nodes in code. |
HighlightOnlyTreeListCellDuringDragOver | Gets or sets whether only one cell, rather than the whole row, is coloured to show where a dragged node would be dropped. |
IsChildDropDownShowing | Gets whether a date cell is being edited and its drop-down calendar is open. |
LineDrawingColor | Gets or sets the colour of the lines that join the nodes. |
LineDrawingDashStyle | Gets or sets the pattern of the lines that join the nodes: solid, dotted, dashed and so on. |
NodeImage | Gets or sets the image shown beside every node's text, between the expand glyph and the text. |
Nodes | Gets the top-level nodes. This is where the tree is built. |
NodesSortColumn | Gets the column the tree is sorted by. |
NodesSortDirection | Gets the direction the tree is sorted in. |
NodesSortOrder | Gets or sets the order of sibling rows in a tree built from HierarchicalDataSource
with ShouldEnableLinkedRelationshipsTuning on: ascending or descending by
UID. |
OptimiseLayoutRenderingDuringDragDrop | Gets or sets whether layout is suspended while a dropped branch is moved, so that a large branch moves without the grid laying itself out for every row. |
OrientationAspectForRenderingColumnHeader | Gets or sets the direction of the gradient the built-in renderer fills the column headings with. |
OrientationAspectForRenderingRow | Gets or sets the direction of the gradient the built-in renderer fills each cell of a row with, and of the hover highlight. |
OrientationAspectForRenderingRowHeader | Gets or sets the direction of the gradient the built-in renderer fills the row headings with. |
RowSelectionBackColorDuringDragOver | Gets or sets the background colour of the row a dragged node is over – the node it would be dropped on. |
RowSelectionForeColorDuringDragOver | Gets or sets the text colour of the row a dragged node is over. |
RowTemplate | Gets or sets the row the grid copies for new rows. |
Rows | Gets the grid's rows: the nodes that are on show, in display order. |
ShouldDrawCellBorderWhenSelected | Gets or sets whether the current cell is outlined with a border of its own while it is not being edited. |
ShouldDrawColumnBackgroundUponCellSelection | Gets or sets whether the column of the current cell is tinted in
ColumnBackgroundOnCellSelectionColor. |
ShouldDrawColumnBackgroundUponMouseHovering | Gets or sets whether the column under the pointer is tinted in
ColumnBackgroundOnMouseHoverColor. |
ShouldDrawFrame | Gets or sets whether the control draws its own frame: a border with rounded corners, an inner padding and, optionally, a soft drop shadow, all inside its bounds. |
ShouldDrawFrameShadow | Gets or sets whether the frame casts a soft drop shadow. |
ShouldEnableFastCaching | Gets or sets whether binding makes a single pass over the data, rather than
DepthForPreparingNodesWithChildren passes. |
ShouldEnableLinkedRelationshipsTuning | Gets or sets whether rows from HierarchicalDataSource are placed in the order of their
UID rather than the order of the table. |
ShowNodeImage | Gets or sets whether the top-level nodes show their image. |
SpecialColumnCellsToDisplayEditingControlOnCellsAlways | Gets or sets whether cells of the special editing columns show their editor's chrome on every row, not only on the cell being edited. |
StartColorForRenderingColumnHeader | Gets or sets the colour at the top or left of the gradient the built-in renderer fills the column headings with. |
StartColorForRenderingRow | Gets or sets the colour at the top or left of the gradient the built-in renderer fills each cell of the rows with. |
StartColorForRenderingRowHeader | Gets or sets the colour at the top or left of the gradient the built-in renderer fills the row headings with. |
UseBuiltInRendering | Gets or sets whether the control paints the parts named by
BuiltInCustomRenderedTargetElement itself, in gradients of the colours you choose,
instead of leaving them to the grid. |
VirtualMode | Gets or sets whether the grid asks for cell values as it needs them rather than holding them. |
| Methods | |
About | Shows the control's About box as a modal dialog. |
BindToTreeListView | Builds the tree from a table now, without setting HierarchicalDataSource. |
ClearAllNodes | Removes every node, at every level, and every row from the control. |
CollapseAllNodes | Closes every node in the tree, at every level, leaving only the top-level nodes on show. |
CollapseNode | Closes a node and, if asked, every node beneath it. |
CollapseNode | Closes a node, taking its descendants off the grid. |
Dispose | Releases the nodes' rows and the control's other resources. |
ExpandAllNodes | Opens every node in the tree, at every level. |
ExpandNode | Opens a node and, if asked, every node beneath it. |
ExpandNode | Opens a node, showing its children. |
GetAllSiblingsForThisNode | Returns the nodes that share a parent with the given node. |
GetFlatListOfNodes | Gets every node in the tree, at every level, as one flat sequence. |
GetFlatListOfNodes | Returns every node beneath a node, at every level, as one flat sequence. |
GetNodeAt | Returns the node shown at a point in the control. |
OnCellMouseDoubleClick | Raises the DataGridView.CellMouseDoubleClick event, then opens or closes the node
when ExpandCollapseNodeOnDoubleClick is on and the cell is in the tree column. |
OnCellMouseDown | Raises the DataGridView.CellMouseDown event, noting where a pressed column heading
stood, so that a heading dragged to a new place is not also taken as a click to sort. |
OnCellMouseEnter | Raises the DataGridView.CellMouseEnter event, and moves the hover highlight to the
row the pointer has entered. |
OnCellMouseLeave | Raises the DataGridView.CellMouseLeave event, and clears the hover highlight from
the row the pointer has left. |
OnCellPainting | Raises the DataGridView.CellPainting event, then paints the parts the control draws
itself: the built-in gradients and hover highlight, the sort glyph and the current cell's border. |
OnColumnAdded | Raises the DataGridView.ColumnAdded event, after making the new column
NotSortable so that sorting is the control's own
(see AllowUserToSortColumns). |
OnColumnHeaderMouseClick | Raises the DataGridView.ColumnHeaderMouseClick event, after sorting by the column
when AllowUserToSortColumns is on. |
OnColumnWidthChanged | Raises the DataGridView.ColumnWidthChanged event, then paints any part of the control
waiting to be painted. |
OnDataError | Raises the DataGridView.DataError event when
AllowDataErrorExceptionToBeThrown is on; otherwise ignores the error. |
OnFontChanged | Raises the FontChanged event, and resizes rows that still have their default
height so that the new font fits them. |
OnHandleCreated | Raises the Control.HandleCreated event, and sizes rows that still have the
default height to fit the control's font. |
OnKeyDown | Raises the Control.KeyDown event, and handles the control's own keys: Shift+Right
and Shift+Left to open and close the current node, F2 to edit the current cell, and Enter to
return to whole-row selection. |
OnMouseDown | Raises the Control.MouseDown event, notes the node under the pointer for
ItemDrag, and restores CellEditingMode when the current cell is not
in the tree column. |
OnMouseLeave | Raises the Control.MouseLeave event, and clears any hover highlight. |
OnMouseMove | Raises the Control.MouseMove event, and raises ItemDrag when a node is
dragged far enough. |
OnMouseUp | Raises the Control.MouseUp event. |
OnParentChanged | Raises the Control.ParentChanged event, and starts watching the new parent's size
so that the scroll bars are laid out again when it changes. |
OnRowEnter | Raises the DataGridView.RowEnter event, and returns the grid to whole-row
selection, selecting the row entered. |
OnRowsAdded | Raises the DataGridView.RowsAdded event, after giving each node added its indent,
and a height to suit the control's font. |
OnSizeChanged | Raises the Control.SizeChanged event. |
SetUpNodeControlType | Gives every node now in the tree a check box, a radio button, or neither. |
SortNodesByColumn | Sorts the tree by the values in a column – siblings within their parent, at every level, closed
branches included – or, with SortOrder.None, puts back the order the nodes were
built in. |
WndProc | Processes the Windows messages sent to the control's window, reserving and painting the frame
when ShouldDrawFrame is on. |
| Events | |
BinaryTreeListViewCellClicked | Occurs when the user presses the mouse button on a cell of the tree column, before the control acts on the click. |
BinaryTreeListViewNodeCollapsed | Occurs after a node has closed and the rows beneath it have been taken out of the grid. |
BinaryTreeListViewNodeCollapsing | Occurs before an open node closes, whether the user closed it or code did; the closing can be cancelled. |
BinaryTreeListViewNodeExpanded | Occurs after a node has opened and its children have been added to the grid. |
BinaryTreeListViewNodeExpanding | Occurs before a node opens, whether the user opened it or code did; the opening can be cancelled. |
ItemDrag | Occurs when the user starts dragging a node, for drag and drop you handle yourself. |
Constructors
BinaryTreeListView
Creates a tree list view with no columns and no nodes.
Remarks
The control starts set up as a tree: whole-row selection, no row headers and no cell
borders, rows that the user cannot add or delete, columns that fill the width
(Fill) and that the user can resize and reorder, and
F2 to edit.
Add a BinaryTreeListViewColumn and any other columns to
DataGridView.Columns, then add nodes through Nodes.
Properties
AllowDataErrorExceptionToBeThrown
Gets or sets whether a cell value that cannot be formatted or committed is reported as a
DataGridView normally reports it.
- Value
falseby default.
Remarks
With it off, such errors are ignored: DataGridView.DataError is not raised, no
error dialog is shown and nothing is thrown.
With it on, the grid's own handling applies: DataGridView.DataError is raised,
the grid shows its error dialog when nothing handles the event, and a handler can set
ThrowException to have the exception thrown.
See also
AllowUserToAddRows
Gets or sets whether the grid shows the row for adding a new row at the bottom.
- Value
falseby default. InControlDataDisplayMode.TreeListViewmode it staysfalsewhatever is set; nodes are added throughNodes.
See also
AllowUserToDeleteRows
Gets or sets whether the user can delete rows with the Delete key.
- Value
falseby default. InControlDataDisplayMode.TreeListViewmode it staysfalsewhatever is set; remove nodes throughNodes.
See also
AllowUserToSortColumns
Gets or sets whether clicking a column heading sorts the tree by that column: ascending, then descending, then back to the order the nodes were built in.
- Value
falseby default.- Default
false
Remarks
A plain row sort would scramble a tree, so this is not the grid's own sort – every column
stays NotSortable. Siblings are sorted within their
parent, at every level, and each parent keeps its children beneath it. The sorted heading shows a
chevron pointing up or down. A heading dragged to a new place is moved, not sorted.
Turning it off while the tree is sorted puts the original order back. To sort from code,
call SortNodesByColumn; to decide how two values compare, handle
DataGridView.SortCompare.
See also
BuiltInCustomRenderedTargetElement
Gets or sets which parts of the grid the built-in renderer paints: the column headings, the row headings, the rows, or a combination.
- Value
BuiltInCustomRenderedTargetElement.ColumnAndRowHeadersby default.
Remarks
Applies while UseBuiltInRendering is on. The rows are painted only for
BuiltInCustomRenderedTargetElement.Row and
BuiltInCustomRenderedTargetElement.All, so those are the settings that
show the hover highlight and the cell borders in CellBorderDrawingColor. Row
headings are hidden in a tree unless you turn on DataGridView.RowHeadersVisible.
Changing it does not repaint the control; call Control.Invalidate to see the change at once.
See also
BusyIndicatorText
Gets or sets the message shown in the middle of the control while
HierarchicalDataSource is being bound.
- Value
- "Processing data... please wait..." by default. Setting
nullor an empty string is ignored and keeps the current message.
Remarks
The message, its colours and its font are read each time binding starts. It is not shown by
BindToTreeListView.
See also
BusyIndicatorTextBackColor
Gets or sets the background colour of the message shown while HierarchicalDataSource
is being bound.
- Value
SystemColors.Windowby default.
See also
BusyIndicatorTextFont
Gets or sets the font of the message shown while HierarchicalDataSource is being
bound.
- Value
CaptionFontby default.
See also
BusyIndicatorTextForeColor
Gets or sets the text colour of the message shown while HierarchicalDataSource is
being bound.
- Value
SystemColors.WindowTextby default.
See also
CellBorderColor
Gets or sets the colour of the border round the current cell.
- Value
Color.Emptyby default, which draws nothing; set a colour to see the border.
See also
CellBorderDrawingColor
Gets or sets the colour of the cell borders the built-in renderer draws on the rows.
- Value
SystemColors.WindowTextby default.
Remarks
Drawn only when UseBuiltInRendering is on,
BuiltInCustomRenderedTargetElement paints the rows
(BuiltInCustomRenderedTargetElement.Row or
BuiltInCustomRenderedTargetElement.All), and
DataGridView.CellBorderStyle is one of
Single,
SingleHorizontal or
SingleVertical. A tree starts with no cell borders
(None), so set the border style as well.
Changing it does not repaint the control; call Control.Invalidate to see the change at once.
CellBorderWidth
Gets or sets the width of the border round the current cell.
- Value
- In pixels; 0 by default, which draws the thinnest line. The width is used as given, not scaled for the display.
See also
CellEditingMode
Gets or sets how the user starts editing a cell. Set this instead of the grid's
DataGridView.EditMode.
- Value
EditOnKeystrokeOrF2by default.
Remarks
The control changes DataGridView.EditMode itself while the user works in the
tree column, so that a click on a node's glyph or check box never starts an edit, and puts this
value back afterwards. A value set on DataGridView.EditMode directly is therefore
overwritten; one set here is kept, and applies at once.
Until this is set, or the user first clicks a cell, the grid starts editing on F2 only.
Example
// Only F2 starts an edit, so typing while a row is selected never changes a value by accident.
plan.CellEditingMode = DataGridViewEditMode.EditOnF2;CollapseImage
Gets or sets the image drawn as the collapse glyph of an open node.
- Value
null, the default, for the system tree glyph.
Remarks
Takes effect only when ExpandImage is set as well. The image is drawn 10 by 10
logical pixels. A node's own BinaryTreeListViewNode.CollapseImage takes precedence.
See also
ColumnBackgroundDefaultCellStyleBackColor
Gets or sets the background colour a tinted column is given back when the pointer, or the current cell, leaves it.
- Value
- The grid's
DataGridView.DefaultCellStyleback colour when the control is created –SystemColors.Windowunless changed.
Remarks
Set it to the column colour you want when nothing is tinted, if that is not the grid's default cell colour.
See also
ColumnBackgroundOnCellSelectionColor
Gets or sets the colour the column of the current cell is tinted in.
- Value
Color.Emptyby default, which leaves the column in the grid's own background colour.
See also
ColumnBackgroundOnMouseHoverColor
Gets or sets the colour the column under the pointer is tinted in.
- Value
Color.Emptyby default, which leaves the column in the grid's own background colour.
See also
ColumnHeaderBottomBorderColor
Gets or sets the colour of a rule drawn along the bottom of the column headings, parting them from the rows.
- Value
Color.Empty, the default, for no rule. The rule is one logical pixel thick.- Default
Remarks
Drawn when UseBuiltInRendering is on and
BuiltInCustomRenderedTargetElement includes the column headings. The built-in
renderer paints its own background over the grid's heading border, so without a rule the headings
end wherever their colour stops. Setting it repaints the control.
See also
ColumnSeparatorDrawingColor
Gets or sets the colour of the line the built-in renderer draws between column headings.
- Value
SystemColors.WindowTextby default.
Remarks
Drawn when DrawColumnSeparatorWhenUsingBuiltinRendering is on and the built-in
renderer paints the column headings. The line runs down the right-hand edge of each heading, two
logical pixels short of its top and bottom. Changing it does not repaint the control; call Control.Invalidate to see the change at once.
See also
ControlDataDisplayMode
Gets or sets whether the control works as a tree list view or as a plain grid with this control's appearance features.
- Value
ControlDataDisplayMode.TreeListView, the default, orControlDataDisplayMode.ListView.
Remarks
Set it before adding columns or rows. In ControlDataDisplayMode.ListView
mode a BinaryTreeListViewColumn cannot be added (adding one throws), setting
HierarchicalDataSource is ignored, and AllowUserToAddRows and
AllowUserToDeleteRows can be turned on.
The settings the constructor makes for a tree – whole-row selection, no row headers, no cell
borders – are not undone by switching to ControlDataDisplayMode.ListView;
set those grid properties yourself.
CurrentNode
Gets the node of the current cell – the row the user is on.
- Value
- The current node, or
nullwhen there is no current cell.
Remarks
Handle DataGridView.SelectionChanged or DataGridView.CurrentCellChanged
to follow it as the user moves.
DepthForPreparingNodesWithChildren
Gets or sets how many passes binding makes over HierarchicalDataSource to place rows
whose parent comes after them in the table.
- Value
- 25 by default.
Remarks
A table that lists every parent before its children needs one pass; the default allows for
a tree up to 25 levels deep listed in any order. Rows not placed after the last pass are left out.
ShouldEnableFastCaching overrides it with a single pass.
Setting it binds the data again. With no HierarchicalDataSource set, that
removes every node from the control – nodes added in code included – so set it before adding
nodes, or only on a bound tree.
See also
DrawColumnSeparatorWhenUsingBuiltinRendering
Gets or sets whether the built-in renderer draws a line between column headings.
- Value
falseby default.
Remarks
Applies while UseBuiltInRendering is on and
BuiltInCustomRenderedTargetElement includes the column headings. The line is drawn in
ColumnSeparatorDrawingColor. Changing it does not repaint the control; call Control.Invalidate to see the change at once.
See also
DrawGlassBackgroundHighlightingOnMouseEnter
Gets or sets whether the row under the pointer is highlighted.
- Value
trueby default.
Remarks
The highlight is part of the built-in row painting, so it shows only while
UseBuiltInRendering is on and BuiltInCustomRenderedTargetElement is
BuiltInCustomRenderedTargetElement.Row or
BuiltInCustomRenderedTargetElement.All. It is a gradient from
GlassBrushTopHalfStartColor to GlassBrushBottomHalfEndColor, in the
direction of OrientationAspectForRenderingRow. It is not drawn on selected rows or
during a drag.
See also
DrawNodeConnectingLines
Gets or sets whether lines are drawn joining each node to its parent and its siblings.
- Value
trueby default.- Default
true
See also
EnableAutomaticBuiltInDragDrop
Gets or sets whether the user can rearrange the tree by dragging nodes with the mouse.
- Value
falseby default. Setting it also setsControl.AllowDropto the same value.
Remarks
With it on, the user presses on a node and drags; the row under the pointer is highlighted in
RowSelectionBackColorDuringDragOver and RowSelectionForeColorDuringDragOver,
with a line beneath it. Dropping on a node makes the dragged node, and everything beneath it, the
last child of that node; dropping on the empty area below the last row makes it the last top-level
node. A drop on the node itself or on one of its own descendants does nothing, and Escape cancels
the drag.
The branch is moved as a copy: new BinaryTreeListViewNode objects with the same
cell values, check state and glyph images replace the old ones, and arrive closed. Look the
nodes up again after a drop rather than keeping references to them.
For drag and drop of your own, leave this off, set Control.AllowDrop, and handle
ItemDrag.
Example
// Let the user reorganise the plan by dragging tasks under other tasks.
plan.EnableAutomaticBuiltInDragDrop = true;
plan.ExpandTargetDropNodeUponDragDrop = true;
plan.RowSelectionBackColorDuringDragOver = Color.FromArgb(204, 228, 247);
plan.RowSelectionForeColorDuringDragOver = Color.Black;See also
EnableCommitingEditWhenMovingFocusFromEditContext
Gets or sets whether an edit in progress is committed when the user clicks the glyph, check box or image area of a tree cell.
- Value
trueby default.
Remarks
With it on, a click on the left-hand part of a tree cell – the indent, the expand glyph, the check box or the image – ends the current edit and keeps the new value before acting on the click. With it off, the edit stays open.
EnableExpandCollapsingOfNodesUponMouseClickOnReadOnlyTreeListViewNodeCellOrColumn
Gets or sets whether, in a read-only tree, a click anywhere in a node's tree cell opens or closes the node.
- Value
trueby default.
Remarks
It applies when the grid is read-only, or the current cell, its row or its column is. A click on the tree cell of a node that has children then toggles it, wherever in the cell the click lands, and the click goes no further – a check box in that cell does not change.
When nothing is read-only, or this is off, only a click on the expand glyph toggles a node
(and a double-click, see ExpandCollapseNodeOnDoubleClick).
EndColorForRenderingColumnHeader
Gets or sets the colour at the bottom or right of the gradient the built-in renderer fills the column headings with.
- Value
SystemColors.Controlby default.
Remarks
The gradient runs from StartColorForRenderingColumnHeader to EndColorForRenderingColumnHeader in the direction
OrientationAspectForRenderingColumnHeader gives; set both to the same colour for a flat fill. It is painted while
UseBuiltInRendering is on and BuiltInCustomRenderedTargetElement
includes the column headings. Changing it does not repaint the control; call Control.Invalidate to see the change at once.
EndColorForRenderingRow
Gets or sets the colour at the bottom or right of the gradient the built-in renderer fills each cell of the rows with.
- Value
SystemColors.Windowby default.
Remarks
The gradient runs from StartColorForRenderingRow to EndColorForRenderingRow in the direction
OrientationAspectForRenderingRow gives; set both to the same colour for a flat fill. It is painted while
UseBuiltInRendering is on and BuiltInCustomRenderedTargetElement
includes the rows. Changing it does not repaint the control; call Control.Invalidate to see the change at once.
EndColorForRenderingRowHeader
Gets or sets the colour at the bottom or right of the gradient the built-in renderer fills the row headings with.
- Value
SystemColors.Controlby default.
Remarks
The gradient runs from StartColorForRenderingRowHeader to EndColorForRenderingRowHeader in the direction
OrientationAspectForRenderingRowHeader gives; set both to the same colour for a flat fill. It is painted while
UseBuiltInRendering is on and BuiltInCustomRenderedTargetElement
includes the row headings. Changing it does not repaint the control; call Control.Invalidate to see the change at once.
ExpandCollapseNodeOnDoubleClick
Gets or sets whether double-clicking a node's cell in the tree column opens or closes the node.
- Value
trueby default.- Default
true
Remarks
Only nodes with children respond, and not while the cell is being edited. A click on the expand glyph toggles a node whatever this is set to.
ExpandCollapsingOfNodesLevel
Gets or sets where the focus must be for Shift+Right and Shift+Left to open and close a node.
- Value
ExpandCollapsingOfNodesLevel.Rowby default.
Remarks
ExpandCollapsingOfNodesLevel.Row: the keys work on the current node whichever of its cells has the focus.ExpandCollapsingOfNodesLevel.TreeListViewCell: the keys work only while the node's cell in the tree column has the focus, leaving them to the other cells otherwise.ExpandCollapsingOfNodesLevel.None: the keyboard does not open or close nodes.
Shift+Right opens a closed node that has children; Shift+Left closes an open one.
ExpandImage
Gets or sets the image drawn as the expand glyph of a closed node that has children.
- Value
null, the default, for the system tree glyph.
Remarks
Takes effect only when CollapseImage is set as well; with either one missing, the
system glyph is used for both. The image is drawn 10 by 10 logical pixels. A node's own
BinaryTreeListViewNode.ExpandImage takes precedence.
Example
plan.ExpandImage = Properties.Resources.ChevronRight; // shown on a closed node
plan.CollapseImage = Properties.Resources.ChevronDown; // shown on an open nodeSee also
ExpandTargetDropNodeUponDragDrop
Gets or sets whether the node a dragged node is dropped on is opened, with everything beneath it, so that the moved node can be seen in its new place.
- Value
falseby default. A node dropped on a closed node is then out of sight until that node is opened.
See also
FrameBorderColor
Gets or sets the colour of the frame's border.
- Value
- A cool grey, RGB 196, 204, 216, by default.
- Default
196, 204, 216
Remarks
Used while ShouldDrawFrame is on, and replaced by
FrameFocusedBorderColor while the focus is in the control. Setting it repaints the
frame at once.
See also
FrameBorderThickness
Gets or sets the thickness of the frame's border.
- Value
- In logical pixels, from 0 to 8; 1 by default. A value outside that range is clamped to it, and 0 draws no border line.
- Default
1
See also
FrameCornerRadius
Gets or sets the radius of the frame's rounded corners.
- Value
- In logical pixels, from 0 to 32; 6 by default. A value outside that range is clamped to it, and 0 gives square corners.
- Default
6
Remarks
The radius is never drawn larger than half the frame's shorter side. A larger radius also needs
more padding to keep the square grid inside the curve; see FramePadding.
See also
FrameFocusedBorderColor
Gets or sets the colour of the frame's border while the focus is in the control – on the grid or on a cell's editor.
- Value
Color.Empty, the default, to keepFrameBorderColorwhether or not the control has the focus.- Default
Remarks
An accent colour here shows the user which grid the keyboard is in, as a focused text box does.
See also
FramePadding
Gets or sets the space between the frame's border and the grid inside it.
- Value
- In logical pixels, from 0 to 16; 3 by default. A value outside that range is clamped to it.
- Default
3
Remarks
The padding used is never less than three tenths of FrameCornerRadius, which is what
keeps the grid's square corners inside the rounded ones. It is filled with
DataGridView.BackgroundColor.
See also
FrameShadowColor
Gets or sets the colour of the frame's drop shadow.
- Value
Color.Blackby default.- Default
Black
Remarks
The shadow's strength is FrameShadowOpacity, so the colour's own alpha is not used.
See also
FrameShadowDepth
Gets or sets how far the frame's shadow spreads.
- Value
- In logical pixels, from 0 to 24; 6 by default. A value outside that range is clamped to it, and 0 draws no shadow.
- Default
6
Remarks
A third of the depth is also the distance the shadow drops below the frame, so it is deeper at the bottom than at the top.
See also
FrameShadowOpacity
Gets or sets the strength of the frame's shadow where it is darkest, next to the border.
- Value
- A percentage, from 0 to 100; 16 by default. A value outside that range is clamped to it, and 0 draws no shadow.
- Default
16
See also
GlassBrushBottomHalfEndColor
Gets or sets the colour the hover highlight ends with – at the bottom of the row, or at its right
when OrientationAspectForRenderingRow is horizontal.
- Value
Color.LightSalmonby default. A colour with no opacity, such asColor.Empty, is replaced by the row's own colour washed a little towards the system highlight colour.
Example
// A pale blue hover band over plain rows.
plan.BuiltInCustomRenderedTargetElement = BuiltInCustomRenderedTargetElement.All;
plan.GlassBrushTopHalfStartColor = Color.FromArgb(235, 243, 252);
plan.GlassBrushBottomHalfEndColor = Color.FromArgb(214, 230, 248);See also
GlassBrushBottomHalfStartColor
Gets or sets a colour kept for compatibility; the hover highlight does not use it.
- Value
Color.Whiteby default.
Remarks
The highlight is a two-colour gradient from GlassBrushTopHalfStartColor to
GlassBrushBottomHalfEndColor.
GlassBrushLinearGradientFactor
Gets or sets a value kept for compatibility; the hover highlight does not use it.
- Value
- 90 by default.
Remarks
The direction of the highlight's gradient follows OrientationAspectForRenderingRow.
GlassBrushTopHalfEndColor
Gets or sets a colour kept for compatibility; the hover highlight does not use it.
- Value
Color.Whiteby default.
Remarks
The highlight is a two-colour gradient from GlassBrushTopHalfStartColor to
GlassBrushBottomHalfEndColor.
GlassBrushTopHalfStartColor
Gets or sets the colour the hover highlight starts with – at the top of the row, or at its left
when OrientationAspectForRenderingRow is horizontal.
- Value
Color.LightSalmonby default. A colour with no opacity, such asColor.Empty, is replaced by the row's own colour washed a little towards the system highlight colour.
See also
HierarchicalDataSource
Gets or sets a table to build the whole tree from, in place of adding nodes in code.
Exceptions
System.Exception- The value is not a
DataTable, aDataViewornull; orDataGridView.DataSourceis set.
Remarks
The table's shape
The first three columns are for the control, and must be named UID, ID and
ParentID; the columns after them are the data, matched to the control's columns in order –
the table's fourth column fills the control's first column, and so on. Add the control's columns
first, at least as many as there are data columns.
| Column | Meaning |
|---|---|
| The row's identity, unique across the whole table. |
| Not used to build the tree, but the column must exist. |
| The |
Values are compared and shown as text: every cell value becomes a string, so a column of
numbers sorts as text unless you handle DataGridView.SortCompare.
Binding
Setting the property removes every node and builds the tree again, showing a busy message
(BusyIndicatorText) while it works; the nodes start closed. Later changes to the
table are not followed – set the property again to show them. Setting it to
null removes every node. With ControlDataDisplayMode set to
ControlDataDisplayMode.ListView the property is ignored; bind the grid's own
DataGridView.DataSource instead.
Rows are placed in passes, in table order unless ShouldEnableLinkedRelationshipsTuning
is on: each pass adds the top-level rows and every row whose parent is already in the tree. List
parents before their children and one pass places everything; otherwise up to
DepthForPreparingNodesWithChildren passes are made. In pass n (counting from
0), a row not yet placed whose ParentID is n - 1 is added as a top-level row, so a
table that lists children before parents should not use small numbers such as 0 and 1 as UIDs of
parent rows.
Example
An organisation chart from a table. Each person's ParentID is their manager's
UID.
DataTable staff = new DataTable("Staff");
staff.Columns.Add("UID", typeof(int));
staff.Columns.Add("ID", typeof(int));
staff.Columns.Add("ParentID", typeof(int));
staff.Columns.Add("Name", typeof(string));
staff.Columns.Add("Role", typeof(string));
staff.Columns.Add("Location", typeof(string));
staff.Rows.Add(1, 1, -1, "Helen Ward", "Chief Executive", "London");
staff.Rows.Add(2, 1, 1, "Marcus Bell", "Finance Director", "London");
staff.Rows.Add(3, 2, 1, "Aisha Khan", "Engineering Director", "Manchester");
staff.Rows.Add(4, 1, 3, "Owen Price", "Platform Lead", "Manchester");
staff.Rows.Add(5, 2, 3, "Lena Fischer", "Mobile Lead", "Berlin");
org.Columns.Add(new BinaryTreeListViewColumn { HeaderText = "Name" });
org.Columns.Add(new DataGridViewTextBoxColumn { HeaderText = "Role" });
org.Columns.Add(new DataGridViewTextBoxColumn { HeaderText = "Location" });
org.HierarchicalDataSource = staff;
org.ExpandAllNodes();See also
HighlightOnlyTreeListCellDuringDragOver
Gets or sets whether only one cell, rather than the whole row, is coloured to show where a dragged node would be dropped.
- Value
falseby default, to colour the whole row.
Remarks
With it on, only the row's first cell is coloured – the tree cell when the tree column is the first column.
See also
IsChildDropDownShowing
Gets whether a date cell is being edited and its drop-down calendar is open.
- Value
truewhile the current cell is in aBinaryTreeListViewDatePickerColumn, is being edited, and its calendar is showing; otherwisefalse.
Remarks
Useful in a key or focus handler of your own, to leave the keys to the calendar while it is open.
LineDrawingColor
Gets or sets the colour of the lines that join the nodes.
- Value
SystemColors.WindowTextby default. The lines are one pixel wide.
Remarks
A colour between the text and the background – Color.Silver on a white grid, for
instance – keeps the lines from competing with the data.
See also
LineDrawingDashStyle
Gets or sets the pattern of the lines that join the nodes: solid, dotted, dashed and so on.
See also
NodeImage
Gets or sets the image shown beside every node's text, between the expand glyph and the text.
- Value
null, the default, for no shared image.
Remarks
A node shows the image set on it through BinaryTreeListViewNode.Image if it
has one, and this image otherwise. Setting this property also sets the
BinaryTreeListViewNode.Image of each top-level node there is at that moment, replacing
theirs – so set it before giving top-level nodes images of their own.
The image is drawn at its own pixel size, not scaled; a 16 by 16 image suits the default row height, which is not changed to fit a larger one. A node with a check box or radio button shows that instead.
Example
accounts.NodeImage = Properties.Resources.Ledger16; // every account
accounts.Nodes[0].Image = Properties.Resources.Bank16; // except the bank accountsSee also
Nodes
Gets the top-level nodes. This is where the tree is built.
- Value
- The collection of top-level nodes. It is never
null.
Remarks
Add the columns first. BinaryTreeListViewNodesCollection.Add() takes
one value per column, in column order, creates the node and returns it; add its children to its
own BinaryTreeListViewNode.Nodes in the same way, to any depth. A top-level node is
on show as soon as it is added; a child is on show once its parent is open.
Pass a value that is not null for the tree column on every node;
Add can throw a NullReferenceException otherwise.
Example
// Columns: Account (the tree column), Code, Balance.
BinaryTreeListViewNode assets = ledger.Nodes.Add("Assets", "1000", 182500.00m);
BinaryTreeListViewNode bank = assets.Nodes.Add("Bank", "1100", 42750.00m);
bank.Nodes.Add("Current account", "1110", 39250.00m);
bank.Nodes.Add("Savings account", "1120", 3500.00m);
assets.Nodes.Add("Equipment", "1500", 139750.00m);
assets.Expand();See also
BinaryTreeListViewNodeFlattenedNodesClearAllNodes
NodesSortColumn
Gets the column the tree is sorted by.
- Value
- The sorted column, or
nullwhen the nodes are in the order they were built in.
See also
NodesSortDirection
Gets the direction the tree is sorted in.
- Value
SortOrder.AscendingorSortOrder.Descendingwhile sorted byNodesSortColumn;SortOrder.None, the default, when it is not sorted.
NodesSortOrder
Gets or sets the order of sibling rows in a tree built from HierarchicalDataSource
with ShouldEnableLinkedRelationshipsTuning on: ascending or descending by
UID.
- Value
NodeSortOrder.Ascendingby default.
Remarks
NodeSortOrder.Descending orders by descending UID; any other value by
ascending UID. Without ShouldEnableLinkedRelationshipsTuning, rows keep the
table's order and this has no effect.
Setting it binds the data again when the control has columns and a
HierarchicalDataSource. Nodes added in code keep the order they were added in; to sort
by a column's values, see SortNodesByColumn.
OptimiseLayoutRenderingDuringDragDrop
Gets or sets whether layout is suspended while a dropped branch is moved, so that a large branch moves without the grid laying itself out for every row.
- Value
falseby default.
See also
OrientationAspectForRenderingColumnHeader
Gets or sets the direction of the gradient the built-in renderer fills the column headings with.
- Value
Vertical, top to bottom, by default.
Remarks
Changing it does not repaint the control; call Control.Invalidate to see the change at once.
See also
OrientationAspectForRenderingRow
Gets or sets the direction of the gradient the built-in renderer fills each cell of a row with, and of the hover highlight.
- Value
Vertical, top to bottom, by default.
Remarks
Each cell is filled on its own, so a horizontal gradient repeats in every cell.
Changing it does not repaint the control; call Control.Invalidate to see the change at once.
See also
OrientationAspectForRenderingRowHeader
Gets or sets the direction of the gradient the built-in renderer fills the row headings with.
- Value
Horizontal, left to right, by default.
Remarks
Changing it does not repaint the control; call Control.Invalidate to see the change at once.
See also
RowSelectionBackColorDuringDragOver
Gets or sets the background colour of the row a dragged node is over – the node it would be dropped on.
- Value
Color.LightSalmonby default.
Remarks
Applies to the built-in drag and drop (EnableAutomaticBuiltInDragDrop). See
HighlightOnlyTreeListCellDuringDragOver for whether the whole row or one cell is
coloured.
RowSelectionForeColorDuringDragOver
Gets or sets the text colour of the row a dragged node is over.
- Value
Color.Blackby default.
See also
RowTemplate
Gets or sets the row the grid copies for new rows.
- Value
- A
BinaryTreeListViewNode, set by the constructor. Its height is raised to fit the control's font when the font is larger than the framework default.
Remarks
Change its Height or DefaultCellStyle
to change every new node; replacing it with a row that is not a
BinaryTreeListViewNode breaks the tree.
Rows
Gets the grid's rows: the nodes that are on show, in display order.
- Value
- The base grid's row collection. Each row is a
BinaryTreeListViewNode.
Remarks
Do not add or remove rows here; the control keeps this collection in step with
Nodes as nodes are added and as they open and close. Rows inside a closed node are
not in it.
See also
NodesFlattenedNodes
ShouldDrawCellBorderWhenSelected
Gets or sets whether the current cell is outlined with a border of its own while it is not being edited.
- Value
falseby default.
Remarks
The border is drawn in CellBorderColor, CellBorderWidth pixels wide, in
place of the grid's dotted focus rectangle. With whole-row selection it marks the cell the
keyboard is on – the one F2 edits.
Example
plan.ShouldDrawCellBorderWhenSelected = true;
plan.CellBorderColor = Color.SteelBlue;
plan.CellBorderWidth = 2;ShouldDrawColumnBackgroundUponCellSelection
Gets or sets whether the column of the current cell is tinted in
ColumnBackgroundOnCellSelectionColor.
- Value
falseby default.
Remarks
The tint follows the column of the current cell. In a tree, where a click selects the whole row, that is the column that was clicked – and, before anything has been clicked, the first column, since the grid makes the first cell current when it is filled. When both column tints apply to the same column, the hover tint wins.
See also
ShouldDrawColumnBackgroundUponMouseHovering
Gets or sets whether the column under the pointer is tinted in
ColumnBackgroundOnMouseHoverColor.
- Value
falseby default.
Remarks
The tint is set as the column's DataGridViewColumn.DefaultCellStyle back
colour, and put back to ColumnBackgroundDefaultCellStyleBackColor as the pointer
leaves. It is hidden while the built-in renderer paints the rows, which fills the cells itself.
Example
// Help the eye follow a column down a wide plan.
plan.ColumnBackgroundOnMouseHoverColor = Color.FromArgb(246, 249, 253);
plan.ShouldDrawColumnBackgroundUponMouseHovering = true;See also
ShouldDrawFrame
Gets or sets whether the control draws its own frame: a border with rounded corners, an inner padding and, optionally, a soft drop shadow, all inside its bounds.
- Value
falseby default.- Default
false
Remarks
The frame replaces DataGridView.BorderStyle: turning it on sets that to
BorderStyle.None, and turning it off puts back what it was. It is painted in the
control's non-client area, so the headings, the rows and the scroll bars all sit inside it and
nothing is drawn over the data.
Outside the border – the shadow and the rounded corners – the control paints the background
colour of the nearest parent that has an opaque one, so place it on a parent of a single colour.
Inside the border, the padding is DataGridView.BackgroundColor.
The border turns FrameFocusedBorderColor while the focus is in the control.
The other settings are FrameBorderColor, FrameBorderThickness,
FrameCornerRadius, FramePadding, ShouldDrawFrameShadow,
FrameShadowColor, FrameShadowDepth and
FrameShadowOpacity.
Example
plan.ShouldDrawFrame = true;
plan.FrameCornerRadius = 8;
plan.FrameBorderColor = Color.FromArgb(200, 208, 220);
plan.FrameFocusedBorderColor = Color.FromArgb(0, 120, 212);
plan.FrameShadowDepth = 8;
plan.FrameShadowOpacity = 12;
plan.BackgroundColor = Color.White; // the padding inside the borderShouldDrawFrameShadow
Gets or sets whether the frame casts a soft drop shadow.
- Value
trueby default.- Default
true
Remarks
The shadow is drawn inside the control's bounds, so it takes room from the grid on every side,
about FrameShadowDepth logical pixels.
See also
ShouldEnableFastCaching
Gets or sets whether binding makes a single pass over the data, rather than
DepthForPreparingNodesWithChildren passes.
- Value
falseby default.
Remarks
One pass places every row whose parent row comes before it, so it is all a table needs when parents
are listed before their children (or, with ShouldEnableLinkedRelationshipsTuning, have
smaller UIDs). A row the single pass cannot place is left out. It applies the next time the data
is bound.
See also
ShouldEnableLinkedRelationshipsTuning
Gets or sets whether rows from HierarchicalDataSource are placed in the order of their
UID rather than the order of the table.
- Value
falseby default.
Remarks
With it on, every UID must be a whole number (a FormatException is
thrown otherwise); rows are taken in ascending UID order – descending when
NodesSortOrder is NodeSortOrder.Descending – so siblings appear in that
order, and a row whose UID repeats an earlier one is left out. When every parent has a
smaller UID than its children, the whole tree is built in one pass whatever the order of the
table.
It applies the next time the data is bound.
See also
ShowNodeImage
Gets or sets whether the top-level nodes show their image.
- Value
trueby default.
Remarks
Setting it sets BinaryTreeListViewNode.ShowNodeImage on each top-level node there is
at that moment, and repaints. Nested nodes, and nodes added afterwards, keep their own setting
(true unless changed); set BinaryTreeListViewNode.ShowNodeImage on
them to hide theirs.
See also
SpecialColumnCellsToDisplayEditingControlOnCellsAlways
Gets or sets whether cells of the special editing columns show their editor's chrome on every row, not only on the cell being edited.
- Value
trueby default.
Remarks
With it on, a BinaryTreeListViewDatePickerColumn cell draws its drop-down
button, a BinaryTreeListViewNumericUpDownColumn cell its up and down buttons, and a
BinaryTreeListViewOffice2007ColorPickerColumn cell its border and drop-down button,
so the user can see at a glance which cells are editable and how. Each of those columns'
DrawStyleIsFlat chooses between a flat chevron and the classic themed button.
With it off, those cells look like plain text cells until they are edited. A colour cell
still shows its swatch. The setting is read as the cells paint; call
Control.Invalidate after changing it to see the change at once.
StartColorForRenderingColumnHeader
Gets or sets the colour at the top or left of the gradient the built-in renderer fills the column headings with.
- Value
SystemColors.Controlby default.
Remarks
The gradient runs from StartColorForRenderingColumnHeader to EndColorForRenderingColumnHeader in the direction
OrientationAspectForRenderingColumnHeader gives; set both to the same colour for a flat fill. It is painted while
UseBuiltInRendering is on and BuiltInCustomRenderedTargetElement
includes the column headings. Changing it does not repaint the control; call Control.Invalidate to see the change at once.
StartColorForRenderingRow
Gets or sets the colour at the top or left of the gradient the built-in renderer fills each cell of the rows with.
- Value
SystemColors.Windowby default.
Remarks
The gradient runs from StartColorForRenderingRow to EndColorForRenderingRow in the direction
OrientationAspectForRenderingRow gives; set both to the same colour for a flat fill. It is painted while
UseBuiltInRendering is on and BuiltInCustomRenderedTargetElement
includes the rows. Changing it does not repaint the control; call Control.Invalidate to see the change at once.
StartColorForRenderingRowHeader
Gets or sets the colour at the top or left of the gradient the built-in renderer fills the row headings with.
- Value
SystemColors.Controlby default.
Remarks
The gradient runs from StartColorForRenderingRowHeader to EndColorForRenderingRowHeader in the direction
OrientationAspectForRenderingRowHeader gives; set both to the same colour for a flat fill. It is painted while
UseBuiltInRendering is on and BuiltInCustomRenderedTargetElement
includes the row headings. Changing it does not repaint the control; call Control.Invalidate to see the change at once.
UseBuiltInRendering
Gets or sets whether the control paints the parts named by
BuiltInCustomRenderedTargetElement itself, in gradients of the colours you choose,
instead of leaving them to the grid.
- Value
trueby default. With the default colours the headings are painted in a flatSystemColors.Control.
Remarks
Each part is filled with a linear gradient from its start colour to its end colour, in the direction its orientation property gives, and its text and selection are then drawn over it:
| Part | Colours and direction |
|---|---|
Column headings |
|
Row headings |
|
Rows |
|
Changing it does not repaint the control; call Control.Invalidate to see the change at once.
Example
// Headings in a soft blue gradient with a rule beneath them; rows left to the grid.
plan.UseBuiltInRendering = true;
plan.BuiltInCustomRenderedTargetElement = BuiltInCustomRenderedTargetElement.ColumnHeader;
plan.StartColorForRenderingColumnHeader = Color.FromArgb(244, 248, 252);
plan.EndColorForRenderingColumnHeader = Color.FromArgb(222, 232, 244);
plan.ColumnHeaderBottomBorderColor = Color.FromArgb(160, 180, 205);
plan.Invalidate();See also
VirtualMode
Gets or sets whether the grid asks for cell values as it needs them rather than holding them.
- Value
falseby default.
Remarks
The same as VirtualMode, except that setting it repaints the control.
Nodes hold their own values, so a tree has no use for it.
Methods
About
Shows the control's About box as a modal dialog.
Remarks
It backs the About entry on the control's design-time menu; an application has no need to call it.
BindToTreeListView
Builds the tree from a table now, without setting HierarchicalDataSource.
Parameters
dataSource- A
DataTableorDataViewshaped asHierarchicalDataSourcedescribes. depth- The number of passes to make over the rows (see
DepthForPreparingNodesWithChildren); 0 or less only clears the tree. WithShouldEnableFastCachingon, one pass is made whatever this is.
Exceptions
System.ExceptiondataSourceis not aDataTableorDataView, or has no columns, anddepthis at least 1.
Remarks
Every node is removed first. No busy message is shown, and
HierarchicalDataSource is left as it was, so a later change to
NodesSortOrder or DepthForPreparingNodesWithChildren does not rebind this
table.
See also
ClearAllNodes
Removes every node, at every level, and every row from the control.
Remarks
The columns stay. Use this rather than Nodes.Clear(), which leaves the rows on show.
See also
CollapseAllNodes
Closes every node in the tree, at every level, leaving only the top-level nodes on show.
Remarks
Nested nodes are closed too, so opening a top-level node afterwards shows its children closed.
BinaryTreeListViewNodeCollapsing and BinaryTreeListViewNodeCollapsed are
raised for each node that was open.
See also
CollapseNode
Closes a node and, if asked, every node beneath it.
Parameters
startNodeToCollapse- The node to close. It must be in this control.
performDeepCollapsetrueto close every descendant as well, so that the branch reopens one level at a time;falseto close onlystartNodeToCollapse, leaving its children as they were for when it reopens.
Returns
Always true.
Remarks
Raises BinaryTreeListViewNodeCollapsing and BinaryTreeListViewNodeCollapsed
for each node that was open.
See also
CollapseNode
Closes a node, taking its descendants off the grid.
Parameters
binaryTreeListViewNode- The node to close. It must be in this control.
Returns
true when the node was closed; false when it was already
closed or a BinaryTreeListViewNodeCollapsing handler cancelled it.
Remarks
Raises BinaryTreeListViewNodeCollapsing and then
BinaryTreeListViewNodeCollapsed. Children keep their own open state for when the node
is opened again; to close them as well, use CollapseNode().
See also
Dispose
Releases the nodes' rows and the control's other resources.
Parameters
disposingtruewhen called fromDispose;falsewhen called from the finalizer.
ExpandAllNodes
Opens every node in the tree, at every level.
Remarks
Each node is opened through BinaryTreeListViewNode.Expand(), so
BinaryTreeListViewNodeExpanding and BinaryTreeListViewNodeExpanded are
raised for each one. A node whose opening a handler cancels stays closed.
Example
expandAllButton.Click += (sender, e) => accounts.ExpandAllNodes();
collapseAllButton.Click += (sender, e) => accounts.CollapseAllNodes();See also
ExpandNode
Opens a node and, if asked, every node beneath it.
Parameters
startNodeToExpand- The node to open. It must be in this control.
performDeepExpansiontrueto open every descendant as well, from the top down;falseto open onlystartNodeToExpand.
Returns
Always true.
Remarks
Each node is opened as ExpandNode() opens it, raising
BinaryTreeListViewNodeExpanding and BinaryTreeListViewNodeExpanded; a
node a handler keeps closed stays closed, and the return value does not report it.
Example
// Open the whole branch the user is on.
if (plan.CurrentNode != null)
plan.ExpandNode(plan.CurrentNode, true);See also
ExpandNode
Opens a node, showing its children.
Parameters
binaryTreeListViewNode- The node to open. It must be in this control.
Returns
true when the node was opened; false when it was already
open or a BinaryTreeListViewNodeExpanding handler cancelled it.
Remarks
Raises BinaryTreeListViewNodeExpanding and then
BinaryTreeListViewNodeExpanded. Children that were themselves open before are shown
open.
Open a node only when its parent is open, or when it is a top-level node: the children of a
node whose parent is closed are placed at the top of the grid. To open a branch at any depth, use
ExpandNode(), which works from the top down.
See also
GetAllSiblingsForThisNode
Returns the nodes that share a parent with the given node.
Parameters
node- The node whose siblings are wanted. For a top-level node, the siblings are the other top-level nodes.
excludeThisNodetrueto leavenodeitself out of the list;falseto include it, in its place.
Returns
A new list, in the order the nodes appear under their parent. It is empty for a node that is not in a tree.
Exceptions
System.NullReferenceExceptionnodeisnull.
GetFlatListOfNodes
Gets every node in the tree, at every level, as one flat sequence.
- Value
- A new list on each call, in tree order: each node followed by its descendants. Nodes inside closed branches are included.
Example
// Tasks not yet finished, at any depth, whether or not their branch is open.
int outstanding = plan.FlattenedNodes
.Count(n => !n.HasChildren && ((WorkItem)n.Tag).PercentDone < 100);See also
GetFlatListOfNodes
Returns every node beneath a node, at every level, as one flat sequence.
Parameters
startNodeWhoseChildrenCollectionAreNeededInTheList- The node whose descendants are wanted. It is not itself in the result.
Returns
A new list in tree order: each child followed by its own descendants. Nodes inside closed branches are included. It is empty for a node with no children.
Exceptions
System.NullReferenceExceptionstartNodeWhoseChildrenCollectionAreNeededInTheListisnull.
See also
FlattenedNodes
GetNodeAt
Returns the node shown at a point in the control.
Parameters
clientPoint- A point in the control's client coordinates, such as the location of a mouse event.
Returns
The node whose row is at that point, or null when the point is on the
headings, below the last row, or outside the control.
Example
// Right-click a task to get its context menu.
plan.MouseUp += (sender, e) =>
{
if (e.Button != MouseButtons.Right)
return;
BinaryTreeListViewNode node = plan.GetNodeAt(e.Location);
if (node != null)
{
plan.CurrentCell = node.Cells[0];
taskMenu.Show(plan, e.Location);
}
};OnCellMouseDoubleClick
Raises the DataGridView.CellMouseDoubleClick event, then opens or closes the node
when ExpandCollapseNodeOnDoubleClick is on and the cell is in the tree column.
Parameters
e- The data for the event.
OnCellMouseDown
Raises the DataGridView.CellMouseDown event, noting where a pressed column heading
stood, so that a heading dragged to a new place is not also taken as a click to sort.
Parameters
e- The data for the event.
OnCellMouseEnter
Raises the DataGridView.CellMouseEnter event, and moves the hover highlight to the
row the pointer has entered.
Parameters
e- The data for the event.
OnCellMouseLeave
Raises the DataGridView.CellMouseLeave event, and clears the hover highlight from
the row the pointer has left.
Parameters
e- The data for the event.
OnCellPainting
Raises the DataGridView.CellPainting event, then paints the parts the control draws
itself: the built-in gradients and hover highlight, the sort glyph and the current cell's border.
Parameters
e- The data for the event.
Remarks
A DataGridView.CellPainting handler runs first. A cell it paints and marks handled is
painted over by the built-in renderer when that renderer owns the cell.
OnColumnAdded
Raises the DataGridView.ColumnAdded event, after making the new column
NotSortable so that sorting is the control's own
(see AllowUserToSortColumns).
Parameters
e- The data for the event.
Exceptions
System.Exception- The column is a
BinaryTreeListViewColumnandControlDataDisplayModeisControlDataDisplayMode.ListView.
OnColumnHeaderMouseClick
Raises the DataGridView.ColumnHeaderMouseClick event, after sorting by the column
when AllowUserToSortColumns is on.
Parameters
e- The data for the event.
Remarks
The sort is done before the event is raised, so a handler sees the new order.
OnColumnWidthChanged
Raises the DataGridView.ColumnWidthChanged event, then paints any part of the control
waiting to be painted.
Parameters
e- The data for the event.
OnDataError
Raises the DataGridView.DataError event when
AllowDataErrorExceptionToBeThrown is on; otherwise ignores the error.
Parameters
displayErrorDialogIfNoHandlertrueto show the grid's error dialog when nothing handles the event.e- What went wrong, and on which cell.
OnFontChanged
Raises the FontChanged event, and resizes rows that still have their default
height so that the new font fits them.
Parameters
e- The data for the event.
OnHandleCreated
Raises the Control.HandleCreated event, and sizes rows that still have the
default height to fit the control's font.
Parameters
e- The data for the event.
OnKeyDown
Raises the Control.KeyDown event, and handles the control's own keys: Shift+Right
and Shift+Left to open and close the current node, F2 to edit the current cell, and Enter to
return to whole-row selection.
Parameters
e- The data for the event.
Remarks
The base grid's key handling runs twice for each key press, so a Control.KeyDown
handler is called twice for the same key. A handler that sets KeyEventArgs.Handled
stops the F2 and Enter handling.
See also
OnMouseDown
Raises the Control.MouseDown event, notes the node under the pointer for
ItemDrag, and restores CellEditingMode when the current cell is not
in the tree column.
Parameters
e- The data for the event.
OnMouseLeave
Raises the Control.MouseLeave event, and clears any hover highlight.
Parameters
e- The data for the event.
Remarks
The pointer can leave the control without leaving a cell first – across the border, or when the window loses the mouse – so the highlight is cleared here as well.
OnMouseMove
Raises the Control.MouseMove event, and raises ItemDrag when a node is
dragged far enough.
Parameters
e- The data for the event.
OnMouseUp
Raises the Control.MouseUp event.
Parameters
e- The data for the event.
OnParentChanged
Raises the Control.ParentChanged event, and starts watching the new parent's size
so that the scroll bars are laid out again when it changes.
Parameters
e- The data for the event.
OnRowEnter
Raises the DataGridView.RowEnter event, and returns the grid to whole-row
selection, selecting the row entered.
Parameters
e- The data for the event.
OnRowsAdded
Raises the DataGridView.RowsAdded event, after giving each node added its indent,
and a height to suit the control's font.
Parameters
e- The data for the event.
OnSizeChanged
Raises the Control.SizeChanged event.
Parameters
e- The data for the event.
SetUpNodeControlType
Gives every node now in the tree a check box, a radio button, or neither.
Parameters
controlTypeNodeControlType.CheckBoxto give every node a ticked check box,NodeControlType.RadioButtonan unselected radio button, orNodeControlType.Defaultto remove them and show images again.
Remarks
It sets BinaryTreeListViewNode.CellInfoCheckStatus on every node at every level
that exists when it is called; nodes added later start with none, so set theirs as you add them.
Check boxes and radio buttons are drawn only on nodes without children, in place of the image,
and the user changes them by clicking. The control is not repainted by this call – call
Control.Invalidate if it is already on show.
Example
// A list of report sections to include, all ticked to begin with.
reports.SetUpNodeControlType(BinaryTreeListView.NodeControlType.CheckBox);
reports.Invalidate();
// Later: the sections still ticked.
List<string> chosen = reports.FlattenedNodes
.Where(n => n.CellInfoCheckStatus == CellInfoCheckStatus.CheckBoxCheckedNormal)
.Select(n => (string)n.Tag)
.ToList();See also
SortNodesByColumn
Sorts the tree by the values in a column – siblings within their parent, at every level, closed
branches included – or, with SortOrder.None, puts back the order the nodes were
built in.
Parameters
column- A column of this control.
orderSortOrder.Ascending,SortOrder.Descending, orSortOrder.Nonefor the original order.
Exceptions
System.ArgumentNullExceptioncolumnisnull.System.ArgumentExceptioncolumnbelongs to another grid, or to none.
Remarks
Values of the same type that can be compared – numbers, dates, strings – compare as that
type; anything else compares by its text in the current culture, ignoring case. Empty values sort
first in ascending order. Nodes that compare equal keep their order. Handle
DataGridView.SortCompare to decide a comparison yourself: the row indices it is
given are -1 for a node that is not on show because its parent is closed.
An edit in progress is committed first, the current node stays current, and the scroll
position is kept. Nodes added after a sort are not placed by it; sort again to include them.
NodesSortColumn and NodesSortDirection report the result, and the
column's heading shows the sort glyph.
Example
// Largest balances first, within each branch.
ledger.SortNodesByColumn(ledger.Columns["Balance"], SortOrder.Descending);
// Sort account codes as numbers, although the cells hold text.
ledger.SortCompare += (sender, e) =>
{
if (e.Column.Name == "Code")
{
e.SortResult = int.Parse((string)e.CellValue1).CompareTo(int.Parse((string)e.CellValue2));
e.Handled = true;
}
};See also
WndProc
Processes the Windows messages sent to the control's window, reserving and painting the frame
when ShouldDrawFrame is on.
Parameters
m- The message.
Remarks
The frame's ring is taken off the window before the grid lays out its headings, rows and scroll bars in what is left, and is painted after the default non-client painting.
Events
BinaryTreeListViewCellClicked
Occurs when the user presses the mouse button on a cell of the tree column, before the control acts on the click.
Remarks
BinaryTreeListViewCellClickedEventArgs.BinaryTreeListViewCellClickedArea says which
part of the cell was clicked: the expand glyph, the check box or radio button, the image, or the
text. Set Cancel to stop what would follow –
the node opening or closing, a check box changing, or the cell starting an edit.
The event comes before a check box changes, so BinaryTreeListViewNode.CellInfoCheckStatus
still holds the old state in the handler. A radio button with no siblings changes before the
event; one with siblings changes after it.
Example
// Open a document when its icon is clicked, and do nothing else with that click.
tree.BinaryTreeListViewCellClicked += (sender, e) =>
{
if (e.BinaryTreeListViewCellClickedArea == BinaryTreeListViewCellClickedArea.Image)
{
OpenDocument((string)e.Node.Tag);
e.Cancel = true;
}
};See also
BinaryTreeListViewNodeCollapsed
Occurs after a node has closed and the rows beneath it have been taken out of the grid.
Remarks
A node keeps its own open or closed state for each of its children, so a child that was open is open again when its parent reopens.
See also
BinaryTreeListViewNodeCollapsing
Occurs before an open node closes, whether the user closed it or code did; the closing can be cancelled.
Remarks
Set Cancel to keep the node open; the call that
asked for it then returns false. It is not raised for a node that is already
closed.
See also
BinaryTreeListViewNodeExpanded
Occurs after a node has opened and its children have been added to the grid.
Remarks
Raised once per node opened, so ExpandAllNodes raises it for every node that had to
open. It is not raised when BinaryTreeListViewNodeExpanding was cancelled.
See also
BinaryTreeListViewNodeExpanding
Occurs before a node opens, whether the user opened it or code did; the opening can be cancelled.
Remarks
Set Cancel to keep the node closed; the
call that asked for it then returns false. Children added to
BinaryTreeListViewNodeExpandingEventArgs.Node inside the handler are shown as the node
opens, which is the way to load a large tree only as it is explored.
It is raised for every expand request on a closed node, including one with no children.
Example
Loading a folder's contents the first time it is opened. Each folder is added with one placeholder child, so that it shows an expand glyph.
tree.BinaryTreeListViewNodeExpanding += (sender, e) =>
{
BinaryTreeListViewNode folder = e.Node;
if (folder.Nodes.Count != 1 || folder.Nodes[0].Tag != null)
return; // already loaded
folder.Nodes.RemoveAt(0); // the placeholder
foreach (string path in Directory.GetFileSystemEntries((string)folder.Tag))
{
BinaryTreeListViewNode child = folder.Nodes.Add(Path.GetFileName(path), File.GetLastWriteTime(path));
child.Tag = path;
}
};See also
ItemDrag
Occurs when the user starts dragging a node, for drag and drop you handle yourself.
Remarks
It is raised when Control.AllowDrop is on, EnableAutomaticBuiltInDragDrop
is off, and the pointer moves beyond the system drag distance with the left button held down over a
node. Item is the BinaryTreeListViewNode pressed on.
Call Control.DoDragDrop from the handler to start the drag.
Example
accounts.AllowDrop = true;
accounts.ItemDrag += (sender, e) =>
{
BinaryTreeListViewNode node = (BinaryTreeListViewNode)e.Item;
accounts.DoDragDrop(node.Tag, DragDropEffects.Copy); // hand the account object to another window
};