using DevExpress.XtraTreeList; using DevExpress.XtraTreeList.Nodes; using Gs.DevApp.Entity; using Gs.DevApp.ToolBox; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Data; using System.Text; using System.Windows.Forms; namespace Gs.DevApp.DevFrm.Work { public partial class UcDictionarySelect : DevExpress.XtraEditors.XtraForm { private List lstCheckedKeyID = new List();//选择局ID集合 string strTitle = ""; string strWhere = ""; public UcDictionarySelect(string _strTitle, string _strWhere) { InitializeComponent(); tlMenu.CustomDrawNodeCheckBox += TreeList1_CustomDrawNodeCheckBox; getTree(); tlMenu.OptionsBehavior.Editable = true; tlMenu.OptionsSelection.EnableAppearanceFocusedCell = false; tlMenu.OptionsSelection.MultiSelect = true; tlMenu.OptionsSelection.MultiSelectMode = TreeListMultiSelectMode.CellSelect; this.strTitle = _strTitle; this.strWhere = _strWhere; this.Text = _strTitle; btnIn.Click += (s, e) => { findOrigin(tlMenu); var list = new List(); foreach (string key in lstCheckedKeyID) { list.Add(new { // dicCode = "001", dicTxt = key }); }; UpdateParent?.Invoke(this, new UpdateParentEventArgs { DynamicList = list }); Close(); }; tlMenu.IndicatorWidth = 50; tlMenu.CustomDrawNodeIndicator += (s, ee) => { if (ee.IsNodeIndicator) { var index = ee.Node.TreeList.GetVisibleIndexByNode(ee.Node); ee.Info.DisplayText = (index + 1).ToString(); } }; } /// /// 选择后的回调事件 /// public event EventHandler UpdateParent; private void TreeList1_CustomDrawNodeCheckBox(object sender, DevExpress.XtraTreeList.CustomDrawNodeCheckBoxEventArgs e) { // 判断当前节点是否为叶子节点(无子节点) if (e.Node.Nodes.Count == 0) { // 允许绘制复选框(默认行为) } else { // 取消绘制复选框 e.Handled = true; } } // 可选:处理节点展开事件,确保动态加载的子节点生效 private void treeList1_BeforeExpand(object sender, BeforeExpandEventArgs e) { // 若子节点是动态加载的,在此处加载数据 // LoadChildNodes(e.Node); } /// /// 读取列表 /// private void getTree() { var pgq = new PageQueryModel(1, 999999, "a.defect_name"); var json = JsonConvert.SerializeObject(pgq); try { var strReturn = UtilityHelper.HttpPost("", "MesDefectCodeManager/GetListPage", json); var dd = UtilityHelper.ReturnToTablePage(strReturn); var dt = dd.rtnData.list; tlMenu.DataSource = dt; tlMenu.KeyFieldName = "guid"; tlMenu.ParentFieldName = "pid"; tlMenu.Tag = "defectName"; tlMenu.EndUpdate(); this.tlMenu.CollapseAll(); // tlMenu.OptionsBehavior.Editable = true; tlMenu.OptionsBehavior.AllowRecursiveNodeChecking = false; tlMenu.BestFitColumns(); } catch (Exception ex) { MsgHelper.Warning("提示:" + ex.Message); } } #region MyRegion /// /// 获取选择状态的数据主键ID集合 /// /// 父级节点 private void GetCheckedKeyID(TreeListNode parentNode) { if (parentNode.Nodes.Count == 0) { return;//递归终止 } foreach (TreeListNode node in parentNode.Nodes) { if (node.CheckState == CheckState.Checked) { DataRowView drv = tlMenu.GetDataRecordByNode(node) as DataRowView;//关键代码,就是不知道是这样获取数据而纠结了很久(鬼知道可以转换为DataRowView啊) if (drv != null) { string KeyFieldName = (string)drv["defectName"]; lstCheckedKeyID.Add(KeyFieldName); } } GetCheckedKeyID(node); } } /// /// 获取选中的节点 /// /// private void findOrigin(DevExpress.XtraTreeList.TreeList tree) { this.lstCheckedKeyID.Clear(); if (tree.Nodes.Count > 0) { foreach (TreeListNode root in tree.Nodes) { GetCheckedKeyID(root); } } } #endregion } }