Treeview绑定以及添加多选功能

代码:

using System;
using System.Data;
using System.Windows.Forms;

namespace Test3.TreeviewTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        
        private void Form1_Load(object sender, EventArgs e)
        {
            DataTable dt_Department = new DataTable();
            dt_Department.Columns.Add("bumenId", typeof(int));
            dt_Department.Columns.Add("部门", typeof(string));
            dt_Department.Rows.Add(1, "产品研发部");
            dt_Department.Rows.Add(2, "工程项目部");
            dt_Department.Rows.Add(3, "行政部");
            dt_Department.Rows.Add(4,"市场部");
            DataSet ds_Department = new DataSet();
            ds_Department.Tables.Add(dt_Department);

            DataTable dt_Employees = new DataTable();
            dt_Employees.Columns.Add("userId", typeof(int));
            dt_Employees.Columns.Add("用户", typeof(string));
            dt_Employees.Columns.Add("bumenId", typeof(int));
            dt_Employees.Rows.Add(1, "张三", 1);
            dt_Employees.Rows.Add(2, "李四", 2);
            dt_Employees.Rows.Add(3, "王五", 3);
            dt_Employees.Rows.Add(4, "赵六", 4);
            dt_Employees.Rows.Add(5, "杨七", 3);
            dt_Employees.Rows.Add(6, "朱八", 4);

            DataSet ds_Employees = new DataSet();
            ds_Employees.Tables.Add(dt_Employees);

            foreach (DataRow dr in ds_Department.Tables[0].Rows)
            {
                //部门表绑定,作为一级层次
                TreeNode tn_origine = new TreeNode();
                tn_origine.Text = dr["部门"].ToString();
                this.treeView1.Nodes.Add(tn_origine);
                //用户表绑定
                DataRow[] dr_arr = ds_Employees.Tables[0].Select("bumenId=" + int.Parse(dr["bumenId"].ToString()));
                if (dr_arr.Length > 0)
                {
                    foreach (DataRow dr_sub in dr_arr)
                    {
                        TreeNode tn_sub = new TreeNode();
                        tn_sub.Text = dr_sub["用户"].ToString();
                        tn_origine.Nodes.Add(tn_sub);
                    }
                }
            }
            this.treeView1.CheckBoxes = true;
        }

        private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
        {
            TreeNode node = e.Node;

            if (e.Action != TreeViewAction.Unknown)
            {
                if (node != null)
                {
                    ChangeChild(node, node.Checked);//影响子节点
                    ChangeParent(node);//影响父节点
                }
            }
        }
        private void ChangeChild(TreeNode node, bool state)
        {
            node.Checked = state;

            foreach (TreeNode tn in node.Nodes)
                ChangeChild(tn, state);
        }

        private void ChangeParent(TreeNode node)
        {
            if (node.Parent != null)
            {
                int brotherNodeCheckedCount = 0;
                foreach (TreeNode tn in node.Parent.Nodes)
                {
                    if (tn.Checked == true)
                        brotherNodeCheckedCount++;
                }
                if (brotherNodeCheckedCount == 0)
                {
                    node.Parent.Checked = false;
                    ChangeParent(node.Parent);
                }
                if (brotherNodeCheckedCount >= 1)
                {
                    node.Parent.Checked = true;
                    ChangeParent(node.Parent);
                }
            }
        }
    }
    
}

 

posted @ 2021-04-07 15:46  当年小清新  阅读(207)  评论(0编辑  收藏  举报