• [DevExpress]TreeListLookUpEdit带checkbox之经典运用


    上代码:

        public partial class TreeListLookUpEdit : DevExpress.XtraEditors.XtraForm
        {
    
            private string _KeyName;
            public string KeyName
            {
                get { return lblKeyName.Text; }
                set { lblKeyName.Text = value; }
            }
            private string _KeyID;
    
            public string KeyID
            {
                get { return lblKeyID.Text; }
                set { lblKeyID.Text= value; }
            }
            public TreeListLookUpEdit()
            {
                InitializeComponent();
            }
            private void TreeListLookUpEdit_Load(object sender, EventArgs e)
            {
            }
    
            protected override void OnLoad(EventArgs e)
            {
                base.OnLoad(e);
    
                if (DesignMode) return;
                txtRole.Properties.TreeList.OptionsView.ShowCheckBoxes = true;
                txtRoleBind();
                DefaultChecked("3");
                GetSelectedRoleIDandName();
    
                txtRole.Properties.TreeList.AfterCheckNode += (s, a) =>
                {
                    a.Node.Selected = true;
                    // txtRole.RefreshEditValue();
                    // txtRole.ForceClosePopup();
                    GetSelectedRoleIDandName();
                };
    
            }
            private void GetSelectedRoleIDandName() 
            {
                this.lstCheckedKeyID.Clear();
                this.lstCheckedKeyName.Clear();
    
                if (txtRole.Properties.TreeList.Nodes.Count > 0)
                {
                    foreach (TreeListNode root in txtRole.Properties.TreeList.Nodes)
                    {
                        GetCheckedKeyID(root);
                    }
                }
                lblKeyID.Text = "";
                lblKeyName.Text = "";
                foreach (int id in lstCheckedKeyID)
                {
                    KeyID += id + ",";
                }
    
                foreach (string name in lstCheckedKeyName)
                {
                    KeyName += name + ",";
                }
            }
            private void DefaultChecked(string rid)
            {
                string strSql = " SELECT [MID] FROM [DZ].[dbo].[DZ_RoleMenu]   where RID=" + rid;
                DataTable dt = DbHelperSQL.Query(strSql).Tables[0];
    
                if (txtRole.Properties.TreeList.Nodes.Count > 0)
                {
                    foreach (TreeListNode nd in txtRole.Properties.TreeList.Nodes)
                    {
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            int checkedkeyid = int.Parse(dt.Rows[i][0].ToString());
                            if (txtRole.Properties.TreeList.FindNodeByKeyID(checkedkeyid) != null) 
                            {
                                txtRole.Properties.TreeList.FindNodeByKeyID(checkedkeyid).Checked = true;
                            }
                        }
                    }
                }
    
            }
            private void txtRoleBind()
            {
                DZAMS.BLL.DZ_RoleInfo rf = new BLL.DZ_RoleInfo();
                string where = "1=1  order by PARENTID,ID ASC";
                DataTable tblDatas = rf.GetList(where).Tables[0];
    
    
                //设置字段 
                this.txtRole.Properties.TreeList.KeyFieldName = "ID";
                this.txtRole.Properties.TreeList.ParentFieldName = "PARENTID";
                this.txtRole.Properties.DataSource = tblDatas;
    
                this.txtRole.Properties.ValueMember = "ID";
                this.txtRole.Properties.DisplayMember = "NAME";
            }
            private List<int> lstCheckedKeyID = new List<int>();//选择局ID集合
            private List<string> lstCheckedKeyName = new List<string>();//选择局Name集合
            /// <summary>
            /// 获取选择状态的数据主键ID集合
            /// </summary>
            /// <param name="parentNode">父级节点</param>
            private void GetCheckedKeyID(TreeListNode parentNode)
            {
                if (parentNode.Nodes.Count == 0)
                {
                    return;//递归终止
                }
                if (parentNode.CheckState != CheckState.Unchecked)
                {
                    DataRowView drv = txtRole.Properties.TreeList.GetDataRecordByNode(parentNode) as DataRowView;
                    if (drv != null)
                    {
                        int KeyFieldName = (int)drv["ID"];
                        string DisplayMember = drv["NAME"].ToString();
                        if (!lstCheckedKeyID.Contains(KeyFieldName))
                        {
                            lstCheckedKeyID.Add(KeyFieldName);
                        }
                        if (!lstCheckedKeyName.Contains(DisplayMember))
                        {
                            lstCheckedKeyName.Add(DisplayMember);
                        }
                    }
                }
                foreach (TreeListNode node in parentNode.Nodes)
                {
                    if (node.CheckState != CheckState.Unchecked)
                    {
                        DataRowView drv = txtRole.Properties.TreeList.GetDataRecordByNode(node) as DataRowView;//关键代码,就是不知道是这样获取数据而纠结了非常久(鬼知道能够转换为DataRowView啊)
                        if (drv != null)
                        {
                            int KeyFieldName = (int)drv["ID"];
                            string DisplayMember = drv["Name"].ToString();
                            lstCheckedKeyID.Add(KeyFieldName);
                            lstCheckedKeyName.Add(DisplayMember);
                        }
                    }
                    GetCheckedKeyID(node);
                }
    
            }
       
            private void txtRole_Closed(object sender, DevExpress.XtraEditors.Controls.ClosedEventArgs e)
            {
            }
    
            void txtRole_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
            {
                e.DisplayText = lblKeyName.Text;
            }
        }


  • 相关阅读:
    ReportViewer,RDLC 报表开发之分页
    Mvc2.0 处理自定义错误.
    使用 WPS中粘贴VS里的代码,并整理格式
    Sql2008中添加程序集.
    快速整理列说明.SQL2008.
    ASP.NET MVC在IIS6下部署的小技巧
    MS SQL Server将数据导出Insert语句的存储过程
    在IE6下发生Internet Explorer cannot open the Internet site错误
    Windows7 中配置IIS7的方法(HTTP 错误 404.3 Not Found)
    安卓2.2手动开启APP2SD方法
  • 原文地址:https://www.cnblogs.com/claireyuancy/p/7228242.html
Copyright © 2020-2023  润新知