コード例 #1
0
 private void AddNodes(DataTable dt, Type type)
 {
     DataRow[] dr;
     if (TRoot == null)
     {
         var property = type.Property(TParentId.ToString());
         if (property != null && !property.PropertyType.IsValueType && property.PropertyType != typeof(string))
         {
             TRoot = Activator.CreateInstance(property.PropertyType);
         }
     }
     if (TRoot != null)
     {
         dr = dt.Select(string.Format("[{0}] = '{1}'", TId, TRoot));
         if (dr.Length > 0)
         {
             throw new PawayException("Child node cannot be the same as the root node.");
         }
         dr = dt.Select(string.Format("[{0}] = '{1}'", TParentId, TRoot));
     }
     else
     {
         dr = dt.Select(string.Format("[{0}] is null", TParentId));
     }
     for (var i = 0; i < dr.Length; i++)
     {
         var node = CreateNode(dr[i]);
         Nodes.Add(node);
         AddNodes(dt, node);
     }
 }
コード例 #2
0
        /// <summary>
        /// 刷新Node
        /// </summary>
        public void UpdateNode(object id)
        {
            if (id == null || _dataSource == null || TId == null || TParentId == null)
            {
                return;
            }
            DataTable dt = null;

            if (_dataSource is DataTable)
            {
                dt = _dataSource as DataTable;
            }
            else if (_dataSource is IList list)
            {
                dt = list.ToDataTable();
            }
            if (dt == null)
            {
                return;
            }
            var dr = dt.Select(string.Format("{0} = '{1}'", TId, id));

            if (dr.Length == 1)
            {
                var nodes = Nodes.Find(id.ToString(), true);
                if (nodes.Length == 1)
                {
                    UpdateNode(Nodes, dr[0], id.ToString());
                }
                else if (nodes.Length == 0)
                {
                    var parent = dr[0][TParentId.ToString()].ToString();
                    if (!AddNode(Nodes, dr[0], parent))
                    {
                        AddNode(Nodes, dr[0]);
                    }
                }
            }
            else if (dr.Length == 0)
            {
                DeleteNode(Nodes, id.ToString());
            }
        }