DevExpress TreeList使用教程之绑定多级树

DevExpress TreeList使用教程之绑定多级树

 


 
概述:TreeList控件可以同时显示树结构和其他数据列,即在一个列上建立父子关系展开或收缩,同时还可以显示其他列的内容。在TreeList中同时引入了 Node和 Columns概念, 第一列为树结构, 其余列为数据列, 任何列都可作为树结构列(即拖动到第一列)显示

TreeList控件可以同时显示树结构和其他数据列,即在一个列上建立父子关系展开或收缩,同时还可以显示其他列的内容。

在TreeList中同时引入了 Node和 Columns概念, 第一列为树结构, 其余列为数据列, 任何列都可作为树结构列(即拖动到第一列)显示。

实现多级树---数据源绑定

在TreeList里添加要显示列(第一列为树结构,其他列为数据列),并指定FieldName为数据库列名

指定KeyFieldName属性为主键,ParentFieldName属性为树状分组列。注意 ParentFieldName指定的列如果为空值,树状可能会混乱(只有一个根结点,其他为此节点的子结点)

用TreeList.DataSource = DataTable 绑定数据即可

(以下内容为转载)多选框的三种状态

树形控件是使用频率很高的一种控件。对于属性控件往往需要下面两个功能

1.TreeList带有CheckBox,并且节点要有三种状态(所有的子节点都选中,所有的子节点都没选择,一部分子节点选中)。使用DevXpress的TreeList控件很容易实现这一功能。

设置TreeList.OptionsView.ShowCheckBoxes = true //是否显示CheckBox

设置TreeList.OptionsBehavior.AllowIndeterminateCheckState = true; //设置节点是否有中间状态,即一部分子节点选中,一部分子节点没有选中

设置这两个属性之后就实现了TreeList带有CheckBox,并且节点有三种状态。

2.选中父节点或者子节点相互影响的功能,如选择父节点选择所有子节点。绑定TreeList的两个事件AfterCheckNode和BeforeCheckNode

实现功能的代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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);
}
}

VB.net语言代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
Private Sub trlContact_AfterCheckNode(ByVal sender As System.ObjectByVal 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 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 As Boolean False
Dim state As CheckState
For 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 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.ObjectByVal 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
原文地址:https://www.cnblogs.com/endv/p/4585158.html