《用C#和数据库实现无限级分类法》修正程序

类别:.NET开发 点击:0 评论:0 推荐:

上次写了这篇文章后,马上发觉有个错误,即可能存在Node漏加的情况。因在添加节点时,只有一个循环,当添加节点时,可能父节点还没有添加,即找不到父亲了,这就引起漏加。
真对不起,没有慎重。不过俺平常就是很随便的,一件事没想成熟就去做,但发现错了,一定会订正的。如果你不喜欢我这样,就把文章扔在一边吧。

原来程序:
        /// <summary>
        /// 重设商品分类的 TreeView
        /// ResetSortView() 函数
        /// </summary>
        #region ResetSortView()函数实现
        private void ResetSortView()
        {
            trvSort.Nodes.Clear();
            arrNode.Clear();

            ExNode nd = new ExNode();
            //
            // 添加商品总类
            //
            Sort mySort = new Sort();
            mySort.ID = 0;
            mySort.Name = "商品总类";
            mySort.ParentID = -1;
            mySort.IsEnd =false;
            mySort.Disable = false;            
            nd.Sort = mySort;
            nd.ImageIndex = 0;
            nd.SelectedImageIndex = 0;
            trvSort.Nodes.Add(nd);
            arrNode.Add(nd);
            // 打开数据库
            // 不好意思,我把数据库打开都定义类了,全包装在DBClass内
            // 这样换成SQL SERVER就省力些
            // DataSet这里也封装,为myDB.DBDataSet
            // 懒得定数据库了,如果不熟悉数据库,快学习一下
            string sql = "Select * From MerchandiseSort Order by MerchandiseSortID";
            DBClass myDB = new DBClass();
            myDB.DBOpen();
            myDB.CreateAdapter(sql);
            myDB.FillDataSet();
            //
            // 把数据记录逐一添加到树开上去
            // 错误从这里开始
            // --------------------------------------------------------------------------------------
            for (int i = 1; i <= myDB.DBDataSet.Tables[0].Rows.Count; i++)
            {
                mySort.ID = (int)myDB.DBDataSet.Tables[0].Rows[i-1]["MerchandiseSortID"];
                mySort.Name = myDB.DBDataSet.Tables[0].Rows[i-1]["Name"].ToString();
                mySort.ParentID = (int)myDB.DBDataSet.Tables[0].Rows[i-1]["ParentID"];
                mySort.IsEnd = (bool)myDB.DBDataSet.Tables[0].Rows[i-1]["IsEnd"];
                mySort.Disable = (bool)myDB.DBDataSet.Tables[0].Rows[i-1]["Disable"];
                AddNode(mySort);
            }
            // --------------------------------------------------------------------------------------
            myDB.DBClose();
            trvSort.ExpandAll();

        }

该代码重写,即向TreeView中成功添加一后,从DataSet口中删去该节点记录,并进行新的一次循环,否则DataSet往下查找能添加的记录。程序如下:

修正后的添加节点的子程序:
        /// <summary>
        /// 在treeView中增加Node,并把Node加入到数组,方便查询
        /// </summary>
        private bool AddNode(Sort addSort)
        {
            bool Added = false;

            ExNode parentNode = new ExNode();  // 要挂接的父结点
            ExNode addNode = new ExNode();     // 本结点

            addNode.Sort = addSort;

            if (addNode.Sort.ParentID == 0)
            {
                trvSort.Nodes[0].Nodes.Add(addNode);
                // 使标志设为找到
                Added = true;
                arrNode.Add(addNode);
                addNode.IDPath = "root\\0";
            }
            else
            {
                foreach ( ExNode pNode in arrNode )
                {
                    if ( pNode.Sort.ID == addSort.ParentID)
                    {
                        parentNode = pNode;
                        parentNode.Nodes.Add(addNode);
                        arrNode.Add(addNode);
                        addNode.IDPath = parentNode.IDPath + "\\" + addNode.Sort.ParentID.ToString();
                        // 使标志设为找到
                        Added = true;

                        break;
                    }
                }               
            }        
           
            // 如果没有找到,返回False
            if ( !Added ) return false;
           
            if (addSort.IsEnd)
            {
                if (addSort.Disable)
                {
                    addNode.ImageIndex = 4;
                    addNode.SelectedImageIndex = 4;
                    addNode.ForeColor = SystemColors.GrayText;
                }
                else
                {
                    addNode.ImageIndex = 2;
                    addNode.SelectedImageIndex = 2;
                    addNode.ForeColor = SystemColors.WindowText;
                }
            }
            else
            {
                if (addSort.Disable)
                {
                    addNode.ImageIndex = 3;
                    addNode.SelectedImageIndex = 3;
                    addNode.ForeColor = SystemColors.GrayText;
                }
                else
                {
                    addNode.ImageIndex = 1;
                    addNode.SelectedImageIndex = 1;
                    addNode.ForeColor = SystemColors.WindowText;
                }
            }
                     
            return true;
        }

修正后的显示分类树程序:

        /// <summary>
        /// 重设商品分类的 TreeView
        /// </summary>
        public void ResetSortView()
        {
            trvSort.Nodes.Clear();
            arrNode.Clear();

            ExNode nd = new ExNode();
            //
            // 添加商品总类
            //
            Sort mySort = new Sort();
            mySort.ID = 0;
            mySort.Name = "商品总类";
            mySort.ParentID = -1;
            mySort.IsEnd =false;
            mySort.Disable = false;           
            nd.Sort = mySort;
            nd.ImageIndex = 0;
            nd.SelectedImageIndex = 0;
            trvSort.Nodes.Add(nd);
            arrNode.Add(nd);
            nd.IDPath = "root";

            string sql = "SELECT * FROM MerchandiseSort ORDER BY MerchandiseSortID ASC";
            DBClass myDB = new DBClass();
            myDB.DBOpen();
            myDB.CreateAdapter(sql);
            myDB.FillDataSet();

            //
            // 以下修正后代码,用二重循环,真到DataSet中的记录全部添加为止
            //

            while ( myDB.DBDataSet.Tables[0].Rows.Count > 0 )
            {
                for (int i = 1; i <= myDB.DBDataSet.Tables[0].Rows.Count; i++)
                {
                    mySort.ID = (int)myDB.DBDataSet.Tables[0].Rows[i-1]["MerchandiseSortID"];
                    mySort.Name = myDB.DBDataSet.Tables[0].Rows[i-1]["SortName"].ToString();
                    mySort.ParentID = (int)myDB.DBDataSet.Tables[0].Rows[i-1]["ParentID"];
                    mySort.IsEnd = (bool)myDB.DBDataSet.Tables[0].Rows[i-1]["IsEnd"];
                    mySort.Disable = (bool)myDB.DBDataSet.Tables[0].Rows[i-1]["Disable"];

                    // 如果添加成功,岀删除DataSet中相应记录,并进入新的循环
                    if ( AddNode(mySort) )
                    {
                        myDB.DBDataSet.Tables[0].Rows.RemoveAt(i - 1);
                        myDB.DBDataSet.Tables[0].AcceptChanges();
                        break;
                    }
                }
            }

            myDB.DBClose();
            trvSort.CollapseAll();
            trvSort.Nodes[0].Expand();
        }

本文地址:http://com.8s8s.com/it/it43526.htm