原创 2011年01月18日 18:43:00

       private List<DataRow> list = new List<DataRow>();

        Boolean afterCheck = true;

        /// <summary>
        /// 当已选中或取消树复选框时触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
        {
            if (e.Action == TreeViewAction.ByMouse || e.Action == TreeViewAction.ByKeyboard)
            {
                if (afterCheck)
                {
                    afterCheck = false;
                    if (e.Node == null) return;
                    TreeNode treeNode = e.Node;
                    if (treeNode.Checked)
                    {
                        TreeNode treeNodeTemp = treeNode;
                        #region
                        if (treeNodeTemp.Nodes.Count != 0)
                        {
                            AddMjNodeList(treeNodeTemp, true);
                        }
                        else
                        {
                            if (treeNodeTemp.Tag !=null)
                            list.Add((DataRow)treeNodeTemp.Tag);
                        }
                        #endregion
                        while (treeNodeTemp.Parent != null)
                        {
                            treeNodeTemp = treeNodeTemp.Parent;
                            if (!treeNodeTemp.Checked)
                            {
                                treeNodeTemp.Checked = treeNode.Checked;
                            }
                        }
                    }
                    /// 添加人员: 阿朝
                    /// 添加时间: 20090724
                    else
                    {
                        int state = 0;
                        TreeNode treeNodeTemp = treeNode;
                        #region
                        if (treeNodeTemp.Nodes.Count != 0)
                        {
                            AddMjNodeList(treeNodeTemp, false);
                        }
                        else
                        {
                            if (treeNodeTemp.Tag!=null)
                            DeleteList(list, treeNodeTemp);
                        }
                        #endregion
                        while (treeNodeTemp.Parent != null)
                        {
                            if (CheckPrevOrNextNode(treeNodeTemp))
                            {
                                state = 1;
                            }
                            treeNodeTemp = treeNodeTemp.Parent;
                            treeNodeTemp.Checked = Convert.ToBoolean(state);
                        }
                    }
                    treeViewNodesCheck(treeNode, treeNode.Checked);

                    afterCheck = true;
                }
            }
        }

         /// <summary>
        /// 选中节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            //if (e.Action == TreeViewAction.ByMouse || e.Action == TreeViewAction.ByKeyboard)
            //{
            //    if (afterCheck)
            //    {
            //        afterCheck = false;
            //        //if (!btnSave.Enabled) btnSave.Enabled = true;
            //        //if (!btnCancel.Enabled) btnCancel.Enabled = true;
            //        if (e.Node == null) return;
            //        TreeNode treeNode = e.Node;
            //        if (treeNode.Checked)
            //        {
            //            TreeNode treeNodeTemp = treeNode;
            //            while (treeNodeTemp.Parent != null)
            //            {
            //                treeNodeTemp = treeNodeTemp.Parent;
            //                if (!treeNodeTemp.Checked)
            //                {
            //                    treeNodeTemp.Checked = treeNode.Checked;
            //                }
            //            }
            //        }
            //        /// 添加人员:  阿朝
            //        /// 添加时间: 20090724
            //        else
            //        {
            //            int state = 0;
            //            TreeNode treeNodeTemp = treeNode;

            //            while (treeNodeTemp.Parent != null)
            //            {
            //                if (CheckPrevOrNextNode(treeNodeTemp))
            //                {
            //                    state = 1;
            //                }
            //                treeNodeTemp = treeNodeTemp.Parent;
            //                treeNodeTemp.Checked = Convert.ToBoolean(state);

            //            }
            //        }
            //        treeViewNodesCheck(treeNode, treeNode.Checked);

            //        afterCheck = true;
            //    }
            //}
        }

        #region 树方法

        private void treeViewNodesCheck(TreeNode treeNode, Boolean check)
        {
            foreach (TreeNode treeNodeChild in treeNode.Nodes)
            {
                treeNodeChild.Checked = check;
                treeViewNodesCheck(treeNodeChild, check);
            }
        }

        /// <summary>
        /// 添加人员: 阿朝
        /// 添加时间: 20090724
        /// </summary>
        /// <param name="node"></param>
        private bool CheckPrevOrNextNode(TreeNode node)
        {
            bool falg = false;

            /// 检查同级的前个节点是否选中
            while (node.PrevNode != null)
            {
                if (node.PrevNode.Checked)
                {
                    falg = true;
                    break;
                }
                else
                {
                    node = node.PrevNode;
                }
            }
            /// 检查同级节点的后一个节点是否选中
            while (node.NextNode != null)
            {
                if (node.NextNode.Checked)
                {
                    falg = true;
                    break;
                }
                else
                {
                    node = node.NextNode;
                }
            }
            return falg;
        }

        #endregion

 

 

  private void DeleteList(List<DataRow> listRow,TreeNode treeNodeNew)
   {
            List<DataRow> listRowCopy = new List<DataRow>();
            foreach (DataRow drownew in listRow)
            {
                listRowCopy.Add(drownew);
            }
            foreach (DataRow drow in listRowCopy)
            {
                DataRow drowNew = (DataRow)treeNodeNew.Tag;
                if (drowNew["sdx"].ToString() == drow["sdx"].ToString())
                {
                    list.Remove(drowNew);
                }
            }
        }

 

        private void AddMjNodeList(TreeNode treeNodeNew,bool falg)
        {

            if (treeNodeNew.Nodes.Count == 0)
            {
                if (falg)
                {
                    list.Add((DataRow)treeNodeNew.Tag);
                }
                else
                {
                    DeleteList(list,treeNodeNew);
                }
            }
            else
            {
                for (int i = 0; i < treeNodeNew.Nodes.Count; i++)
                {
                    AddMjNodeList(treeNodeNew.Nodes[i], falg);
                }
            }
              
                //TreeNode treeNodeNext = treeNodeNew.NextNode;
                //if (treeNodeNext != null)
                //{
                //    AddMjNodeList(treeNodeNext, falg);
                //}
                //TreeNode treeNodeprv = treeNodeNew.PrevNode;
                //if (treeNodeprv != null)
                //{
                //    AddMjNodeList(treeNodeprv, falg);
                //}
        
            //while (treeNodeNew.NextNode != null)
            //{
            //    //TreeNode treeNodeNext = treeNodeNew.NextNode;
            //    //if (treeNodeNext != null)
            //    //{
            //    AddMjNodeList(treeNodeNew.NextNode, falg);
            //    //}
            //}
            //while (treeNodeNew.PrevNode != null)
            //{
            //    //TreeNode treeNodeprv = treeNodeNew.PrevNode;
            //    //if (treeNodeprv != null)
            //    //{
            //    AddMjNodeList(treeNodeNew.PrevNode, falg);
            //    //}
            //}
    
        }

相关文章推荐

线段树 PPT

  • 2017年11月19日 18:02
  • 630KB
  • 下载

二叉树的实现

  • 2017年11月19日 18:10
  • 2KB
  • 下载

二叉查找树(通过外排思想处理大数据)

因为要读的文件很大 ,不能直接一次读进数组,需要通过读一部分,释放内存,再读的方式来解决, 因为直接插入二叉查找(排序)树,则不需要再将排序好的数据先放入别的文件。#include #include...
  • za30312
  • za30312
  • 2017年06月04日 22:06
  • 96

数据结构-非递归遍历二叉树

  • 2017年11月16日 10:28
  • 216KB
  • 下载

二叉树线索化以及线索化的先序、中序、后序遍历

二叉树线索化以及线索化的先序、中序、后序遍历 有详细的图解 和 程序代码 解读,以及 为什么要线索化二叉树...

信号检测与估计理论 赵树杰

  • 2017年11月07日 17:58
  • 18.88MB
  • 下载

关于二叉树后续遍历算法的一点思考

二叉树的三种遍历方式,最贱的那个

决策树算法

  • 2017年11月21日 19:26
  • 382KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:
举报原因:
原因补充:

(最多只允许输入30个字)