《用C#和資料庫實現無限級分類法》修正程式

來源:互聯網
上載者:User

上次寫了這篇文章後,馬上發覺有個錯誤,即可能存在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();
      } 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.