这是DataTreeView.cs的部分代码 #region Member        internal bool hasChanges = false;
        protected bool hasErrors = false;
        protected DataTable dataSource = null;
        private DataTreeViewKeys primaryKeys;
        private DataTreeViewDataSourceType dataSourceType = DataTreeViewDataSourceType.Unknow;
        private DataColumn[] tablePK = null;
        private string nodeTextField = "Name";
#endregion
 private void InitalizeDataTreeView()
        {
            this.primaryKeys = DataTreeViewKeys.Default;            this.ListenNodeOpEvents();
            InitExtendMenu();
        }        internal DataTable GetSrcDataTable()
        {
            return this.dataSource;
        }        #region Extend events        public delegate void DataSourceChangedHandle(object sender, DataTreeViewDataSourceChangeEventArgs e);
        public event DataSourceChangedHandle DataSourceChanged = null;
        #endregion        #region Extend interface        public DataTreeViewKeys PrimaryKeys
        {
            get { return this.primaryKeys; }
            set { this.primaryKeys = value; }
        }        public string NodeTextField
        {
            set
            {
                this.nodeTextField = value;
                if ((this.nodeTextField == null) || (this.nodeTextField == string.Empty))
                {
                    this.nodeTextField = "Name";
                }
            }
            get
            {
                return this.nodeTextField;
            }
        }        public bool HasChanges
        {
            get
            {
                //return this.hasChanges;                if ((this.dataSource != null) && (this.dataSource.GetChanges() != null))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }        public bool HasErrors
        {
            get { return this.hasErrors; }
        }        public virtual void LoadFromXml(string flieName)
        {
            this.dataSourceType = DataTreeViewDataSourceType.XML;            DataSet ds = new DataSet("TreeViewStructure");
            ds.ReadXml(flieName);
            this.DataSource = ds.Tables[0];
        }        public virtual void SaveToXml(string fileName)
        {
            if (this.dataSource == null)
            {
                this.CreateDataSourceStructure();
            }            this.AcceptDataSourceChanges();
            DataSet ds = this.dataSource.DataSet;
            if (ds == null)
            {
                ds = new DataSet("TreeViewStructure");
                ds.Tables.Add(this.dataSource);
            }
            ds.WriteXml(fileName, XmlWriteMode.WriteSchema);
        }        public virtual DataTable DataSource
        {
            get
            {
                if (this.dataSource != null)
                {
                    this.AcceptDataSourceChanges();                    this.ResetTablePK();
                }                return this.dataSource;
            }            set
            {
                this.dataSource = value;                if (this.dataSource == null)
                {
                    if (this.DataSourceChanged != null)
                    {
                        this.DataSourceChanged(this, new DataTreeViewDataSourceChangeEventArgs(DataTreeViewDataSourceType.Unknow));
                    }
                    return;
                }                if ((!this.dataSource.HasErrors) && (this.dataSourceType == DataTreeViewDataSourceType.XML))
                {
                    this.dataSource.AcceptChanges();
                }                this.LoadData();
                this.CancelTablePK();                if (this.DataSourceChanged != null)
                {
                    if (this.dataSourceType == DataTreeViewDataSourceType.Unknow)
                    {
                        this.dataSourceType = DataTreeViewDataSourceType.DataBase;
                    }
                    this.DataSourceChanged(this, new DataTreeViewDataSourceChangeEventArgs(dataSourceType));
                }
            }
        }        public void AcceptDataSourceChanges()
        {
            if (this.dataSource != null)
            {
                this.AcceptChangeToDataSource(this.Nodes);                //this.LoadData();
            }
        }        public void RejectDataSourceChanges()
        {
            if (this.dataSource != null)
            {
                this.dataSource.RejectChanges();                this.LoadData();
            }
        }

解决方案 »

  1.   

    这也是DataTreeView.cs的部分代码public bool CheckTableStructure(DataTable table)
            {
                if (table == null) return false;
                if ((table.Columns[this.primaryKeys.ID] == null)
                    || (table.Columns[this.primaryKeys.ParentID] == null)
                    || (table.Columns[this.primaryKeys.Sequence] == null))
                {
                    return false;
                }            if ((table.Columns[this.primaryKeys.ID].DataType != System.Type.GetType("System.String"))
                    || (table.Columns[this.primaryKeys.ParentID].DataType != System.Type.GetType("System.String"))
                    || ((table.Columns[this.primaryKeys.Sequence].DataType != System.Type.GetType("System.Int64"))
                        && (table.Columns[this.primaryKeys.Sequence].DataType != System.Type.GetType("System.Int32"))
                        && (table.Columns[this.primaryKeys.Sequence].DataType != System.Type.GetType("System.Int16"))))
                {
                    return false;
                }            return true;        }        public DataTable CreateDataSourceStructure()
            {
                this.dataSource = new DataTable("DataTreeViewConfig");            this.dataSource.Columns.Add(this.primaryKeys.ID, System.Type.GetType("System.String"));
                this.dataSource.Columns.Add(this.primaryKeys.ParentID, System.Type.GetType("System.String"));
                this.dataSource.Columns.Add(this.primaryKeys.Sequence, System.Type.GetType("System.Int64"));
                this.dataSource.Columns.Add("Text", System.Type.GetType("System.String"));            return this.dataSource;
            }        public void WriteTreeRelation(string srcField, string destField, string separator)
            {
                if (this.dataSource == null)
                {
                    throw new Exception("DataSource is null!");
                }            if ((this.dataSource.Columns[srcField] == null)
                    || (this.dataSource.Columns[destField] == null))
                {
                    throw new Exception("invalid source field or destination field name!");
                }            if ((this.dataSource.Columns[srcField].DataType != typeof(string))
                    || (this.dataSource.Columns[destField].DataType != typeof(string)))
                {
                    throw new Exception("invalid source field or destination field type!");
                }            separator = (separator == null) ? string.Empty : separator;            foreach (TreeNode node in this.Nodes)
                {
                    this.WriteFullPathInfo(node, srcField, destField, separator);
                }
            }
      public enum DataTreeViewDataSourceType
        {
            Unknow = 0,
            DataBase,
            XML
        }    public class DataTreeViewDataSourceChangeEventArgs : EventArgs
        {
            private DataTreeViewDataSourceType dataSourceType = DataTreeViewDataSourceType.Unknow;
            public DataTreeViewDataSourceChangeEventArgs(DataTreeViewDataSourceType dataSourceType)
                : base()
            {
                this.dataSourceType = dataSourceType;
            }        public DataTreeViewDataSourceType DataSourceType
            {
                get { return this.dataSourceType; }
            }
        }    public struct DataTreeViewKeys
        {
            public DataTreeViewKeys(string idName, string pIdName, string seqName)
            {
                this.ID = idName;
                this.ParentID = pIdName;
                this.Sequence = seqName;
            }        public string ID;
            public string ParentID;
            public string Sequence;        public static DataTreeViewKeys Default
            {
                get
                {
                    DataTreeViewKeys keys = new DataTreeViewKeys();
                    keys.ID = "ID";
                    keys.ParentID = "ParentID";
                    keys.Sequence = "Sequence";                return keys;
                }
            }
      

  2.   

    帖的时候才发现太长了..只贴了DataTreeView.cs的部分代码..
    请高手帮忙看下啊。..谢谢拉.....
      

  3.   

    3楼的,不是DataGridView控件 ...而且也没有那个属性.继续期待高手.....