DevExpress treelist using the tutorial to bind a multi-level tree
Overview:The Treelist control can display both the tree structure and other data columns, that is, to establish a parent-child relationship expansion or contraction on one column, and also to display the contents of other columns. Both the node and columns concepts are introduced in Treelist, the first column is the tree structure, the rest is the column of data, and any column can be displayed as a tree column (that is, dragged to the first row)
.
The Treelist control can display both the tree structure and other data columns, that is, to establish a parent-child relationship expansion or contraction on one column, and also to display the contents of other columns.
The node and columns concepts are introduced in Treelist, the first column is the tree structure, the rest is column columns, and any column can be displayed as a tree structure column (that is, dragged to the first row).
Implementing multi-level tree--data source binding
Add the columns to be displayed in Treelist (the first column is the tree structure, the other columns are the data column), and specify FieldName as the database column name
Specifies that the Keyfieldname property is the primary key, and the Parentfieldname property is a tree-like grouping column. Note If the column specified by Parentfieldname is null, the tree may be confusing (only one root node, and the other child nodes for this node)
Bind data with Treelist.datasource = DataTable
(reproduced below) three states of the multi-marquee
A tree control is a control that uses a high frequency. The following two features are often required for property controls
1.TreeList has a checkbox, and the node has three states (all child nodes are selected, all child nodes are not selected, and a subset of the child nodes are selected). This is easily accomplished with the Devxpress treelist control.
Set TreeList.OptionsView.ShowCheckBoxes = TRUE//Whether the checkbox is displayed
Set TreeList.OptionsBehavior.AllowIndeterminateCheckState = true; Sets whether the node has an intermediate state, that is, a subset of the child nodes are selected, and a subset is not selected
After setting these two properties, the Treelist is implemented with a checkbox and there are three states of the node.
2. Select the parent node or child node that affects each other, such as selecting the parent node to select all child nodes. Two events Afterchecknode and Beforechecknode bound to a treelist
The code to implement the function is as follows:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
private void treeList1_AfterCheckNode(
object sender, DevExpress.XtraTreeList.NodeEventArgs e)
{
SetCheckedChildNodes(e.Node, e.Node.CheckState);
SetCheckedParentNodes(e.Node, e.Node.CheckState);
}
private void treeList1_BeforeCheckNode(
object sender, DevExpress.XtraTreeList.CheckNodeEventArgs e)
{
e.State = (e.PrevState == CheckState.Checked ? CheckState.Unchecked : CheckState.Checked);
}
/// <summary>
/// 设置子节点的状态
/// </summary>
/// <param name="node"></param>
/// <param name="check"></param>
private void SetCheckedChildNodes(TreeListNode node, CheckState check)
{
for (
int i = 0; i < node.Nodes.Count; i++)
{
node.Nodes[i].CheckState = check;
SetCheckedChildNodes(node.Nodes[i], check);
}
}
/// <summary>
/// 设置父节点的状态
/// </summary>
/// <param name="node"></param>
/// <param name="check"></param>
private void SetCheckedParentNodes(TreeListNode node, CheckState check)
{
if (node.ParentNode !=
null
)
{
bool b =
false
;
CheckState state;
for (
int i = 0; i < node.ParentNode.Nodes.Count; i++)
{
state = (CheckState)node.ParentNode.Nodes[i].CheckState;
if (!check.Equals(state))
{
b = !b;
break
;
}
}
node.ParentNode.CheckState = b ? CheckState.Indeterminate : check;
SetCheckedParentNodes(node.ParentNode, check);
}
}
|
The vb.net language code is as follows
123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
Private Sub trlContact_AfterCheckNode(
ByVal sender
As System.
Object
,
ByVal e
As DevExpress.XtraTreeList.NodeEventArgs)
Handles trlContact.AfterCheckNode
SetCheckedChildNodes(e.Node, e.Node.CheckState)
SetCheckedParentNodes(e.Node, e.Node.CheckState)
End Sub
‘设置子结点的状态
Private Sub SetCheckedChildNodes(
ByVal node
As TreeListNode,
ByVal check
As CheckState)
For i
As Integer = 0
To node.Nodes.Count - 1
node.Nodes(i).CheckState = check
SetCheckedChildNodes(node.Nodes(i), check)
Next
End Sub
‘设置父结点的状态
Private Sub SetCheckedParentNodes(
ByVal node
As TreeListNode,
ByVal check
As CheckState)
If node.ParentNode
Is Nothing =
False Then
Dim b
As Boolean =
False
Dim state
As CheckState
For i
As Integer = 0
To node.ParentNode.Nodes.Count - 1
state = node.ParentNode.Nodes(i).CheckState
If check.Equals(state) =
False Then
b =
Not b
Exit For
End If
Next
If b
Then
node.ParentNode.CheckState = CheckState.Indeterminate
Else
node.ParentNode.CheckState = check
End If
SetCheckedParentNodes(node.ParentNode, check)
End If
End Sub
Private Sub trlContact_BeforeCheckNode(
ByVal sender
As System.
Object
,
ByVal e
As DevExpress.XtraTreeList.CheckNodeEventArgs)
Handles trlContact.BeforeCheckNode
If e.PrevState = CheckState.Checked
Then
e.State = CheckState.Unchecked
Else
e.State = CheckState.Checked
End If
End Sub
|
DevExpress treelist using the tutorial to bind a multi-level tree