1. 程式人生 > >DevExpress中TreeList的一個節點改變父節點也要改變的例項

DevExpress中TreeList的一個節點改變父節點也要改變的例項

      最近需要做一個功能:在treelist上進行操作,一旦某個列的值進行了改變,那麼它的父節點的值也需要跟著改變,同時要將其改變的值儲存到資料庫。

      思路是先得到值改變的那個節點,並迴圈找到父節點,在這同時,需要一個儲存節點值得類,把每一個節點的每個屬性儲存到該類的物件中,並使用一個List將其放到列表中,這樣這個List中就可以儲存那個節點和其父節點一直往上的父節點。在將資料儲存到類的物件中時,這時可直接改變樹上的某個節點的值,這樣,既改變了顯示的值,又可以使用List將改變的值寫進資料庫。

     下面是程式碼:

 class InspectionValuation
    {
        private int _inspectionValuationID;
        private int _prjDownListID;
        private string _prjLwfbID;
        private string _rowType;
        private string _prjNodeID;
        private string _teamID;
        private string _prjParentNodeID;
        private string _materialNodeID;
        private string _unit;
        private double _contactAmount;
        private double _price;
        private double _contactValence;
        private double _thisMonthAcount;
        private double _thisMonthValence;
        private double _thisYearAcount;

        private double _thisYearValence;
        private double _fromBeginAcount;
        private double _fromBeginValence;
        private double _surplusAcount;
        private double _surplusValence;
        private int _month;
        private int _year;
        private string _isEndMonth;


        #region 費用清單驗工計價編號
        public int InspectionValuationID
        {
            get { return _inspectionValuationID; }
            set { _inspectionValuationID = value; }
        }
        #endregion
        #region 專案編號
        public int PrjDownListID
        {
            get { return _prjDownListID; }
            set { _prjDownListID = value; }
        }
        #endregion
        #region 本月數量
        public double ThisMonthAcount
        {
            get { return _thisMonthAcount; }
            set { _thisMonthAcount = value; }


        }
        #endregion
        #region 對下專案編號
        public string PrjLwfbID
        {
            get { return _prjLwfbID; }
            set { _prjLwfbID = value; }
        }
        #endregion
        #region 驗工計價行分類
        public string RowType
        {
            get { return _rowType; }
            set { _rowType = value; }
        }
        #endregion
        #region 節點編碼
        public string PrjNodeID
        {
            get { return _prjNodeID; }
            set { _prjNodeID = value; }
        }
        #endregion
        #region 施工隊編號
        public string TeamID
        {
            get { return _teamID; }
            set { _teamID = value; }
        }
        #endregion
        #region 父節點編碼
        public string PrjParentNodeID
        {
            get { return _prjParentNodeID; }
            set { _prjParentNodeID = value; }
        }
        #endregion
        #region 專案名稱
        public string MaterialName
        {
            get { return _materialNodeID; }
            set { _materialNodeID = value; }
        }
        #endregion
        #region 合同單位
        public string Unit
        {
            get { return _unit; }
            set { _unit = value; }
        }
        #endregion
        #region 合同數量
        public double ContactAmount
        {
            get { return _contactAmount; }
            set { _contactAmount = value; }
        }
        #endregion
        #region 合同單價
        public double Price
        {
            get { return _price; }
            set { _price = value; }
        }
        #endregion
        #region 合同金額
        public double ContactValence
        {
            get { return _contactValence; }
            set { _contactValence = value; }
        }
        #endregion
        #region 本月合價
        public double ThisMonthValence
        {
            get { return _thisMonthValence; }
            set { _thisMonthValence = value; }
        }
        #endregion
        #region 本年數量
        public double ThisYearAcount
        {
            get { return _thisYearAcount; }
            set { _thisYearAcount = value; }
        }
        #endregion
        #region 本年合價
        public double ThisYearValence
        {
            get { return _thisYearValence; }
            set { _thisYearValence = value; }
        }
        #endregion
        #region 開累計價數量
        public double FromBeginAcount
        {
            get { return _fromBeginAcount; }
            set { _fromBeginAcount = value; }
        }
        #endregion
        #region 開累合計
        public double FromBeginValence
        {
            get { return _fromBeginValence; }
            set { _fromBeginValence = value; }
        }
        #endregion
        #region 剩餘數量
        public double SurplusAcount
        {
            get { return _surplusAcount; }
            set { _surplusAcount = value; }
        }
        #endregion
        #region 剩餘合價
        public double SurplusValence
        {
            get { return _surplusValence; }
            set { _surplusValence = value; }
        }
        #endregion
        #region 月
        public int Month
        {
            get { return _month; }
            set { _month = value; }
        }
        #endregion
        #region 年
        public int Year
        {
            get { return _year; }
            set { _year = value; }
        }
        #endregion
        #region 是否月末次計價
        public string IsEndMonth
        {
            get { return _isEndMonth; }
            set { _isEndMonth = value; }
        }
        #endregion

    }

下面的程式碼是:當某個單元格的值發生改變時,獲取它之前的值:

 private void treeYgjj_CellValueChanging(object sender, DevExpress.XtraTreeList.CellValueChangedEventArgs e)
        {
            if (e.Column.Name == "thisMonthAcount")
            {
                try
                {

                    iOldValue = Convert.ToDouble(e.Node[thisMonthAcount]);
                }
                catch (Exception ex)
                {
                    iOldValue = 0;

                }
            }
        }


下面程式碼是:當某個單元格的值發生改變後,觸發的事件

private void treeYgjj_CellValueChanged(object sender, DevExpress.XtraTreeList.CellValueChangedEventArgs e)
        {
            if (e.Column.Name == "thisMonthAcount")
            {
                try
                {                   
                    e.Node[thisMonthValence] = (Convert.ToDouble(e.Value) * Convert.ToDouble(e.Node[contactValence])).ToString();

                    double iChangedValue = Convert.ToDouble(e.Value) - iOldValue;

                    if (e.Node.ParentNode != null)
                    {
                        SetCheckedParentNodes(e.Node, iChangedValue);
                    }
                }
                catch (Exception ex)
                {
                    e.Node[thisMonthValence] = 0;
                }
            }
        }
下面的程式碼是實現SetCheckedParentNodes,找尋其父節點並將值改變,並且儲存進List中,寫入資料庫
        private void SetCheckedParentNodes(TreeListNode node, double dataChangeCount)
        {
           
            FindParentNode(node,dataChangeCount);
            SaveChangedDataToDB(listNodeAndParentNode);
         
        }
        /// <summary>
        /// 儲存修改的節點回資料庫
        /// </summary>
        /// <param name="listNodeAndParentNode"></param>
        private void SaveChangedDataToDB(List<InspectionValuation> listNodeAndParentNode)
        {
            
            DataTable Dt = new DataTable();
            #region 為DataTable新增列
            Dt.Columns.Add(new DataColumn("inspectionValuationID", typeof(int)));
            Dt.Columns.Add(new DataColumn("prjDownListID", typeof(int)));
            Dt.Columns.Add(new DataColumn("prjLwfbID", typeof(string)));
            Dt.Columns.Add(new DataColumn("rowType", typeof(string)));
            Dt.Columns.Add(new DataColumn("prjNodeID", typeof(string)));
            Dt.Columns.Add(new DataColumn("teamID", typeof(string)));
            Dt.Columns.Add(new DataColumn("prjParentNodeID", typeof(string)));
            Dt.Columns.Add(new DataColumn("materialName", typeof(string)));
            Dt.Columns.Add(new DataColumn("unit", typeof(string)));
            Dt.Columns.Add(new DataColumn("contactAmount", typeof(Double)));
            Dt.Columns.Add(new DataColumn("price", typeof(Double)));
            Dt.Columns.Add(new DataColumn("contactValence", typeof(Double)));
            Dt.Columns.Add(new DataColumn("thisMonthAcount", typeof(Double)));
            Dt.Columns.Add(new DataColumn("thisMonthValence", typeof(Double)));
            Dt.Columns.Add(new DataColumn("thisYearAcount", typeof(Double)));
            Dt.Columns.Add(new DataColumn("thisYearValence", typeof(Double)));
            Dt.Columns.Add(new DataColumn("fromBeginAcount", typeof(Double)));
            Dt.Columns.Add(new DataColumn("fromBeginValence", typeof(Double)));
            Dt.Columns.Add(new DataColumn("surplusAcount", typeof(Double)));
            Dt.Columns.Add(new DataColumn("surplusValence", typeof(Double)));
            Dt.Columns.Add(new DataColumn("month", typeof(int)));
            Dt.Columns.Add(new DataColumn("year", typeof(int)));
            Dt.Columns.Add(new DataColumn("IsEndMonth", typeof(string)));
            #endregion
            
            //遍歷listNodeAndParentNode將修改過的節點的值儲存回資料庫
            for (int i = 0; i < listNodeAndParentNode.Count; i++)
            {
                tblInspectionValuation tiv = new tblInspectionValuation();
                DataRow dr = dt.NewRow();
                #region 為DataRow賦值
                dr["inspectionValuationID"] = int.Parse(listNodeAndParentNode[i].InspectionValuationID.ToString());
                dr["prjDownListID"] = int.Parse(listNodeAndParentNode[i].PrjDownListID.ToString());
                dr["prjLwfbID"] = listNodeAndParentNode[i].PrjLwfbID;
                dr["rowType"] = listNodeAndParentNode[i].RowType;
                dr["prjNodeID"] = listNodeAndParentNode[i].PrjNodeID;
                dr["teamID"] = listNodeAndParentNode[i].TeamID;
                dr["prjParentNodeID"] = listNodeAndParentNode[i].PrjParentNodeID;
                dr["materialName"] = listNodeAndParentNode[i].MaterialName;
                dr["unit"] = listNodeAndParentNode[i].Unit;
                dr["contactAmount"] = Convert.ToDouble( listNodeAndParentNode[i].ContactAmount );
                dr["price"] = Convert.ToDouble(listNodeAndParentNode[i].Price);
                dr["contactValence"] = Convert.ToDouble(listNodeAndParentNode[i].ContactValence);
                dr["thisMonthAcount"] = Convert.ToDouble(listNodeAndParentNode[i].ThisMonthAcount);
                dr["thisMonthValence"] = Convert.ToDouble(listNodeAndParentNode[i].ThisMonthValence);
                dr["thisYearAcount"] = Convert.ToDouble(listNodeAndParentNode[i].ThisYearAcount);
                dr["thisYearValence"] = Convert.ToDouble(listNodeAndParentNode[i].ThisYearValence);
                dr["fromBeginAcount"] = Convert.ToDouble(listNodeAndParentNode[i].FromBeginAcount);
                dr["fromBeginValence"] = Convert.ToDouble(listNodeAndParentNode[i].FromBeginValence);
                dr["surplusAcount"] = Convert.ToDouble(listNodeAndParentNode[i].SurplusAcount);
                dr["surplusValence"] = Convert.ToDouble(listNodeAndParentNode[i].SurplusValence);
                dr["month"] = int.Parse(listNodeAndParentNode[i].Month.ToString());
                dr["year"] = int.Parse(listNodeAndParentNode[i].Year.ToString());
                dr["IsEndMonth"] = listNodeAndParentNode[i].Month.ToString();
             
                #endregion
                
                tiv.UpdateByPK(dr);
            }
              
        }

        //全域性變數listNodeAndParentNode,儲存修改過的樹節點
        List<InspectionValuation> listNodeAndParentNode = new List<InspectionValuation>();
        int i = 0;
        
        private void FindParentNode(TreeListNode node,double dataChange)
        {
            InspectionValuation iv = new InspectionValuation();
            #region 為InspectionValuation的物件賦值
            iv.InspectionValuationID = int.Parse( node.GetValue(inspectionValuationID).ToString() );
            iv.PrjDownListID = int.Parse(node.GetValue(prjDownListID).ToString());
            iv.RowType = node.GetValue(rowType).ToString();
            iv.PrjLwfbID = node.GetValue(prjLwfbID).ToString();
            iv.PrjNodeID = node.GetValue(prjNodeID).ToString();
            iv.TeamID = node.GetValue(teamID).ToString();
            iv.PrjParentNodeID = node.GetValue(prjParentNodeID).ToString();
            iv.MaterialName = node.GetValue(materialName).ToString();
            iv.Unit = node.GetValue(unit).ToString();
            iv.ContactAmount =Convert.ToDouble( node.GetValue(contactAmount) );
            iv.Price = Convert.ToDouble(node.GetValue(price));
            iv.ContactValence = Convert.ToDouble(node.GetValue(contactValence));
            //如果 i = 1,說明該節點是手動修改的節點,修改過的值就不再加變化的值了,
            //如果是其他節點那麼需要加變化的值,因為i是全域性變數,所以需要在一次修改之後將其值改為0
            if (++i == 1)
            {
                iv.ThisMonthAcount = Convert.ToDouble(node.GetValue(thisMonthAcount)) ;
            }
            else 
            {
                iv.ThisMonthAcount = Convert.ToDouble(node.GetValue(thisMonthAcount)) + dataChange;
            }     
            //下面一句是將修改過的值顯示到節點上
            node[thisMonthAcount] = iv.ThisMonthAcount.ToString();
            iv.ThisMonthValence = Convert.ToDouble(node.GetValue(thisMonthValence));
            iv.ThisYearAcount = Convert.ToDouble(node.GetValue(thisYearAcount));
            iv.ThisYearValence = Convert.ToDouble(node.GetValue(thisYearValence));
            iv.FromBeginAcount = Convert.ToDouble(node.GetValue(fromBeginAcount));
            iv.FromBeginValence = Convert.ToDouble(node.GetValue(fromBeginValence));
            iv.SurplusAcount = Convert.ToDouble(node.GetValue(surplusAcount));
            iv.SurplusValence = Convert.ToDouble(node.GetValue(surplusValence));
            iv.Month = int.Parse(node.GetValue(month).ToString());
            iv.Year = int.Parse(node.GetValue(year).ToString());
            iv.IsEndMonth = node.GetValue(IsEndMonth).ToString();
            #endregion
           //將一個節點上的值用InspectionValuation型別的物件儲存到listNodeAndParentNode中
            listNodeAndParentNode.Add(iv);
            //迴圈找到其父節點
            if (node.ParentNode != null  )
            {
                FindParentNode(node.ParentNode,dataChange);
            }
            //全域性變數重新設為0,如果不設為0,那麼第一個修改的值會重複累加
            i = 0;
        }
      

修改全部完成,雖然專案中沒有用到,但還是一點小case,自己記錄一下,每天都有點進步。

想清楚了再做,否則到最後的時候就可能做不下去。