关闭

标签: nullobjectlist
348人阅读 评论(0) 收藏 举报
分类:

       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);
            //    //}
            //}
    
        }

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:5179次
    • 积分:150
    • 等级:
    • 排名:千里之外
    • 原创:8篇
    • 转载:1篇
    • 译文:0篇
    • 评论:1条
    文章存档