首页 > 代码库 > DevExpress之TreeList复选框实现

DevExpress之TreeList复选框实现

概述:本文主要讲述DevExpress TreeList复选框实现,希望对大家有帮助。

一、TreeList的GetStateImage事件应用

方法一:此事件用于在tree之前设置显示的图片,具体使用方法是首先在窗体上放置imagelist,设置好图片,即可在事件中判断后,选择要显示的图片的索引值即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
private void treeList_GetStateImage(object sender, DevExpress.XtraTreeList.GetStateImageEventArgs e) 
 
  {
 
   CheckState check = GetCheckState(e.Node.Tag);
 
   if(check == CheckState.Unchecked)
 
    e.NodeImageIndex = 0;
 
   else if(check == CheckState.Checked)
 
    e.NodeImageIndex = 1;
 
   else e.NodeImageIndex = 2;
 
  }

方法二:为tree中指定的节点区分现实不同的图片

示例代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
private void roleTree_GetStateImage(object sender, DevExpress.XtraTreeList.GetStateImageEventArgs e)
 
        {
 
            if(e.Node.Id.ToString() == "0")
 
            {
 
                this.roleTree.Nodes[0].RootNode.StateImageIndex = 1;
 
            }
 
            else
 
            {
 
                e.NodeImageIndex = 0;
 
            }
 
        }

二、TreeList中运用MouseDown事件获取所选节点信息

提取tree中的值所用事件为MouseDown,在事件中填写如下代码:

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
private void roleTree_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
 
{
 
if(e.Button == MouseButtons.Left)
 
{
 
DevExpress.XtraTreeList.TreeListHitInfo hInfo = roleTree.GetHitInfo(new Point(e.X, e.Y));
 
if(hInfo.HitInfoType == DevExpress.XtraTreeList.HitInfoType.StateImage)
 
{
 
// this.roleTree.DeleteNode(hInfo.Node);
 
if(MessageBox.Show("你确定要删除"+hInfo.Node[1].ToString()+"角色","提示信 息",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
 
{
 
TB_Accounts_RolesEntity role = new TB_Accounts_RolesEntity();
 
role.iRoleID = Convert.ToInt32(hInfo.Node[1].ToString());
 
role.Retrieve();
 
role.Delete();
 
InitRoleTree();
 
}
 
else
 
{}
 
}
 
else if(hInfo.HitInfoType == DevExpress.XtraTreeList.HitInfoType.Cell)
 
{
 
// MessageBox.Show(hInfo.Node[1].ToString()); //获取选择的iRoleID
 
// SetCheckedNode(treeList.Nodes[0].Nodes[0]);
 
//得到指定iRoleID的全部的vDescript的允许的Table
 
string roleID = hInfo.Node[1].ToString();
 
string sqlString="SELECT dbo.TB_Accounts_Permissions.vDescript FROM dbo.TB_Accounts_Permissions INNER JOIN dbo.TB_Accounts_RolePermissions ON dbo.TB_Accounts_Permissions.iPermissionID = dbo.TB_Accounts_RolePermissions.iPermissionID WHERE 1=1"; //编写SQL语句
 
sqlString +=" and dbo.TB_Accounts_RolePermissions.iRoleID = ‘"+roleID+"‘";
 
//执行SQL语句
 
DataTable dt=Query.ProcessSql(sqlString,"EasySales");
 
for(int i = 0; i<dt.Rows.Count; i++)
 
{
 
string treeText = treeList.GetNodeByVisibleIndex(i).GetDisplayText(0).ToString();
 
if(treeText == dt.Rows[i]["vDescript"].ToString())
 
{
 
SetCheckedNode(treeList.Nodes[0].Nodes[i]);
 
}
 
}
 
/**/////// //判断treeList的行数和DataTable的行数是否一致
 
////// if(this.treeList.VisibleNodesCount.ToString()==dt.Rows.Count.ToString())
 
////// {
 
//////
 
////// //循环treeList的全部的vDescript的值
 
////// for(int i = 0; i<Convert.ToInt32(this.treeList.VisibleNodesCount.ToString());i++)
 
////// {
 
////// //判断treeList的vDescript的值和Table的vDescipte的值是否相等
 
////// if(this.treeList.GetNodeByVisibleIndex(i).GetDisplayText(0).ToString() != dt.Rows[i]["vDescript"].ToString())
 
////// {
 
////// MessageBox.Show("得到不同的vDescript");
 
//////// Checked();
 
////// }
 
////// else
 
////// {
 
////// MessageBox.Show("得到相同的vDescript");
 
//////// UnChecked();
 
////// }
 
////// }
 
////// }
 
////// else
 
////// {
 
////// MessageBox.Show("长度不一致");
 
////// }
 
}
 
}
 
}

三、TreeList前添加CheckBox的选择框,可实现点击父节点相应子节点全部选中的效果

TreeList中并为提供对应实现此功能的CheckBox自定义列,故必须用上面提到的GetStateImage事件添加三个(全选、部分全选、为选中)CheckBox的图片,具体样式可自己设计,然后放到窗体中的ImageList中去,触发及调用用到下面的代码:

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
private CheckState GetCheckState(object obj)
 
{
 
if(obj != null) return (CheckState)obj;
 
return CheckState.Unchecked;
 
}
 
private void SetCheckedNode(string treeText)
 
{
 
CheckState check = new CheckState();
 
check = CheckState.Checked;
 
}
 
private void SetCheckedNode(DevExpress.XtraTreeList.Nodes.TreeListNode node)
 
{
 
CheckState check = GetCheckState(node.Tag);
 
if(check == CheckState.Indeterminate || check == CheckState.Unchecked) check = CheckState.Checked;
 
else check = CheckState.Unchecked;
 
treeList.BeginUpdate();
 
node.Tag = check;
 
SetCheckedChildNodes(node, check);
 
SetCheckedParentNodes(node, check);
 
treeList.EndUpdate();
 
}
 
private void SetCheckedChildNodes(DevExpress.XtraTreeList.Nodes.TreeListNode node, CheckState check)
 
{
 
for(int i = 0; i < node.Nodes.Count; i++)
 
{
 
node.Nodes[i].Tag = check;
 
SetCheckedChildNodes(node.Nodes[i], check);
 
}
 
}
 
private void SetCheckedParentNodes(DevExpress.XtraTreeList.Nodes.TreeListNode node, CheckState check)
 
{
 
if(node.ParentNode != null)
 
{
 
bool b = false;
 
for(int i = 0; i < node.ParentNode.Nodes.Count; i++)
 
{
 
if(!check.Equals(node.ParentNode.Nodes[i].Tag))
 
{
 
b = !b;
 
break;
 
}
 
}
 
node.ParentNode.Tag = b ? CheckState.Indeterminate : check;
 
SetCheckedParentNodes(node.ParentNode, check);
 
}
 
}
 
private void treeList_GetStateImage(object sender, DevExpress.XtraTreeList.GetStateImageEventArgs e)
 
{
 
CheckState check = GetCheckState(e.Node.Tag);
 
if(check == CheckState.Unchecked)
 
e.NodeImageIndex = 0;
 
else if(check == CheckState.Checked)
 
e.NodeImageIndex = 1;
 
else e.NodeImageIndex = 2;
 
}
 
private void treeList_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
 
{
 
if(e.KeyData =http://www.mamicode.com/= Keys.Space)
 
SetCheckedNode(treeList.FocusedNode);
 
}
 
private void treeList_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
 
{
 
if(e.Button == MouseButtons.Left)
 
{
 
DevExpress.XtraTreeList.TreeListHitInfo hInfo = treeList.GetHitInfo(new Point(e.X, e.Y));
 
if(hInfo.HitInfoType == DevExpress.XtraTreeList.HitInfoType.StateImage)
 
SetCheckedNode(hInfo.Node);
 
}
 
}
 
private void roleTree_GetStateImage(object sender, DevExpress.XtraTreeList.GetStateImageEventArgs e)
 
{
 
if(e.Node.Id.ToString() == "0")
 
{
 
this.roleTree.Nodes[0].RootNode.StateImageIndex = 1;
 
}
 
else
 
{
 
e.NodeImageIndex = 0;
 
}
 
}

这样就可实现上述功能。

四、Grid中设置、获取指定Cell的值,用下面的方法:

获取:GridView.GetRowCellValue()
设置:GridView.SetRowCellValue()

DevExpress之TreeList复选框实现