[DevExpress] The classic application of Treelistlookupedit with checkbox

Source: Internet
Author: User



On the code:





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];


            // set field
            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> (); // Select the office ID set
        private List <string> lstCheckedKeyName = new List <string> (); // Select the Office Name collection
        /// <summary>
        /// Get the primary key ID collection for the selected state
        /// </ summary>
        /// <param name = "parentNode"> parent node </ param>
        private void GetCheckedKeyID (TreeListNode parentNode)
        {
            if (parentNode.Nodes.Count == 0)
            {
                return; // Recursive termination
            }
            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; // The key code is that I have been tangled for a long time without knowing how to get the data (the ghost knows that it can be converted to 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;
         }
     } 






[DevExpress] The classic application of Treelistlookupedit with checkbox



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.