http://www.cnblogs.com/xuhaibiao/archive/2011/10/26/2225689.html
Deverpress之TreeList、Grid
一、TreeList的GetStateImage事件应用。
方法一:此事件用于在tree之前设置显示的图片,具体使用方法是首先在窗体上放置imagelist,设置好图片,即可在事件中判断后,选择要显示的图片的索引值即可。
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中指定的节点区分现实不同的图片。
事例代码如下:
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,在事件中填写如下代码
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中去,触发及调用用到下面的代码:
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 == 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()