DevExpress TreeList
1.数据绑定最基本的两个属性:KeyFieldName和ParentFieldName。
SELECT OfficeID,OfficeName,ParentOfficeID FROM tOffice |
这种数据库设计是比较常见的,一般数据满足树形关系就可以这样设计。绑定数据时,只需指定DataSource为对应 的DataTable,指定KeyFieldName为表主键字段,ParentFieldName为表指向主键的外键字段名。
privatevoid BindData() { this.tlOffice.DataSource = dtOffice; tlOffice.KeyFieldName ="OfficeID"; //tlOffice.DataMember = "OfficeName"; tlOffice.Columns["OfficeName"].Caption ="局名称"; tlOffice.ParentFieldName ="ParentOfficeID"; }
绑定数据如下:
为让数据不可编辑,需将OPtionsBehavior→Editable属性设为False:
① . 选择某一节点时,该节点的子节点全部选择 取消某一节点时,该节点的子节点全部取消选择
哪个节点引起行为的?节点是选中还是取消选中?由此确定方法的两个参数:TreeListNode和CheckState。遍历该节点及其子孙,并将其选中状态设置为该节点的状态即可。
///<summary>/// 选择某一节点时,该节点的子节点全部选择 取消某一节点时,该节点的子节点全部取消选择 ///</summary>///<param name="node"></param>///<param name="state"></param>privatevoid 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>privatevoid SetCheckedParentNodes(TreeListNode node, CheckState check) { if (node.ParentNode !=null) { CheckState parentCheckState = node.ParentNode.CheckState; CheckState nodeCheckState; for (int i =0; i < node.ParentNode.Nodes.Count; i++) { nodeCheckState = (CheckState)node.ParentNode.Nodes[i].CheckState; if (!check.Equals(nodeCheckState))//只要任意一个与其选中状态不一样即父节点状态不全选 { parentCheckState = CheckState.Unchecked; break; } parentCheckState = check;//否则(该节点的兄弟节点选中状态都相同),则父节点选中状态为该节点的选中状态 } node.ParentNode.CheckState = parentCheckState; SetCheckedParentNodes(node.ParentNode, check);//遍历上级节点 } }
上两步写好了,别忘了上面的两个方法在TreeList_AfterCheckNode里面触发:
privatevoid tlOffice_AfterCheckNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e) { SetCheckedChildNodes(e.Node, e.Node.CheckState); SetCheckedParentNodes(e.Node, e.Node.CheckState); }
运行效果如下图:
3.获取选中的复选框数据列表
当用户选择一个或多个节点时,要获取对应的数据,将数据表对应主键存到集合中即可。关键是怎样获取数据,当时就纠结了很久。
private List<int> lstCheckedOfficeID =new List<int>();//选择局ID集合///<summary>/// 获取选择状态的数据主键ID集合 ///</summary>///<param name="parentNode">父级节点</param>privatevoid GetCheckedOfficeID(TreeListNode parentNode) { if (parentNode.Nodes.Count ==0) { return;//递归终止 }
foreach (TreeListNode node in parentNode.Nodes) { if (node.CheckState == CheckState.Checked) { DataRowView drv = tlOffice.GetDataRecordByNode(node) as DataRowView;//关键代码,就是不知道是这样获取数据而纠结了很久(鬼知道可以转换为DataRowView啊)if (drv !=null) { int OfficeID = (int)drv["OfficeID"]; lstCheckedOfficeID.Add(OfficeID); } } GetCheckedOfficeID(node); } }
foreach (TreeListNode node in parentNode.Nodes) { if (node.CheckState == CheckState.Checked) { DataRowView drv = tlOffice.GetDataRecordByNode(node) as DataRowView;//关键代码,就是不知道是这样获取数据而纠结了很久(鬼知道可以转换为DataRowView啊)if (drv !=null) { int OfficeID = (int)drv["OfficeID"]; lstCheckedOfficeID.Add(OfficeID); } } GetCheckedOfficeID(node); } }
下面测试获取主键列表:
privatevoid btnCheck_Click(object sender, EventArgs e) { this.lstCheckedOfficeID.Clear();
if (tlOffice.Nodes.Count >0) { foreach (TreeListNode root in tlOffice.Nodes) { GetCheckedOfficeID(root); } }
string idStr =string.Empty; foreach (int id in lstCheckedOfficeID) { idStr += id +""; } MessageBox.Show(idStr); }
if (tlOffice.Nodes.Count >0) { foreach (TreeListNode root in tlOffice.Nodes) { GetCheckedOfficeID(root); } }
string idStr =string.Empty; foreach (int id in lstCheckedOfficeID) { idStr += id +""; } MessageBox.Show(idStr); }
如下图:
4、手动绑定TreeList
#region 填充权限树状TreeList private void BindRoleFunctionGrid(DataSet ds) { TreeListNode parentNode = null; TreeListNode childNode = null; TreeListNode grandsonNode = null; treeList1.BeginUnboundLoad(); treeList1.ClearNodes(); DataTable dt = ds.Tables[0]; //整个权限表数据 DataRow[] drArr = dt.Select("FParentID=0");//查询主树干 DataTable dtNew = dt.Clone(); //复制表模板 for (int i = 0; i < drArr.Length; i++) { dtNew.ImportRow(drArr[i]); } DataView treeDv = dtNew.DefaultView; DataTable parentDt = null; DataTable childDt = null; DataTable grandsonDt = null; parentDt = treeDv.ToTable(); foreach (DataRow parentDr in parentDt.Rows) { TreeListNode treelistnode = null; parentNode = this.treeList1.AppendNode(new object[] { parentDr["FFunctionID"], parentDr["FFunctionName"], parentDr["FParentID"], parentDr["FPermission"], parentDr["FID"], parentDr["FRoleID"] }, treelistnode); DataRow[] drArr2 = dt.Select("FParentID='" + parentDr["FFunctionID"] + "'");//查询父节点下的子节点 DataTable dtNew2 = dt.Clone(); //复制表模板 for (int i = 0; i < drArr2.Length; i++) { dtNew2.ImportRow(drArr2[i]); } DataView treeDv2 = dtNew2.DefaultView;//父节点下的子节点 childDt = treeDv2.ToTable(); foreach (DataRow childDr in childDt.Rows) { childNode = this.treeList1.AppendNode(new object[] { childDr["FFunctionID"], childDr["FFunctionName"], childDr["FParentID"], childDr["FPermission"], childDr["FID"], childDr["FRoleID"] }, parentNode); DataRow[] drArr3 = dt.Select("FParentID='" + childDr["FFunctionID"] + "'");//查询父节点下的子节点下的子节点 DataTable dtNew3 = dt.Clone(); //复制表模板 for (int i = 0; i < drArr3.Length; i++) { dtNew3.ImportRow(drArr3[i]); } DataView treeDv3 = dtNew3.DefaultView;//父节点下的子节点的子节点 grandsonDt = treeDv3.ToTable(); foreach (DataRow grandsonDr in grandsonDt.Rows) { grandsonNode = this.treeList1.AppendNode(new object[] { grandsonDr["FFunctionID"], grandsonDr["FFunctionName"], grandsonDr["FParentID"], grandsonDr["FPermission"], grandsonDr["FID"], grandsonDr["FRoleID"] }, childNode); } } } //treeList1.KeyFieldName = "FFunctionID"; //treeList1.ParentFieldName = "FParentID"; treeList1.EndUnboundLoad(); //treeList1.ExpandAll(); } #endregion
5、根据绑定的表判定treelist节点该不该勾选
#region 如果角色有这个权限则勾选对应的checkedit /// <summary> /// 如果角色有这个权限则勾选对应的checkedit /// </summary> /// <param name="node"></param> /// <param name="ds"></param> private void SetCheckedNodes() { if (treeList1.Nodes.Count > 0) { foreach (TreeListNode root in treeList1.Nodes) { setcheckednodes(root); } } } #endregion private void setcheckednodes(TreeListNode parentNode) { if (parentNode.Nodes.Count == 0) { return;//递归终止 } foreach (TreeListNode node in parentNode.Nodes) { string str = node.GetValue("FPermission").ToString(); //可以直接通过GetValue()这个函数直接获得该节点对应的字段值 if (node.GetValue("FPermission").ToString() == "True") { node.CheckState = CheckState.Checked; } setcheckednodes(node); } //如果父节点下的子节点全部选了,则父节点也勾选 foreach (TreeListNode item in parentNode.Nodes) { SetCheckedParentNodes(item, item.CheckState); } }
http://www.cnblogs.com/sndnnlfhvk/archive/2011/05/15/2046920.html