Table是怎样炼成的:Row、RowCollection

接下来的连续几篇,我们要演练作一个描述通用的二维表,并演示该二维表的继承通途。

先定义数据行的状态


 1    /// <summary>
 2    /// 数据行的状态
 3    /// </summary>

 4    public enum RowState
 5    {
 6        /// <summary>
 7        /// 新建状态
 8        /// </summary>

 9        Added,
10        /// <summary>
11        /// 删除状态
12        /// </summary>

13        Deleted,
14        /// <summary>
15        /// 修改状态
16        /// </summary>

17        Modified,
18        /// <summary>
19        /// 为止状态
20        /// </summary>

21        Unchanged
22    }

开始定义数据行了

  1    /// <summary>
  2    /// 数据行
  3    /// </summary>

  4    public class Row
  5    {
  6        private object[] datas;
  7        private System.Collections.ArrayList colNames;
  8
  9        /// <summary>
 10        /// 该数据行所依赖的表的名称
 11        /// </summary>

 12        public readonly string TableName; 
 13        /// <summary>
 14        /// 该数据行的状态
 15        /// </summary>

 16        public RowState RowState;
 17
 18
 19        protected internal Row(ColumnCollection columns, string tableName)
 20        {
 21            datas = new object[columns.Count];
 22            this.TableName = tableName;
 23            colNames = new System.Collections.ArrayList();
 24            foreach (Column col in columns)
 25            {
 26                colNames.Add(col.ColumnName);
 27            }

 28        }

 29
 30
 31
 32        /// <summary>
 33        /// 获取或设置行的指定列的数据
 34        /// </summary>
 35        /// <param name="index">列的索引号,从0开始</param>
 36        /// <returns>列中存储的数据</returns>

 37        public object this[int index]
 38        {
 39            set
 40            {
 41                datas[index] = value; 
 42                this.RowState = RowState.Modified;
 43            }

 44            get
 45            {
 46                return datas[index];
 47            }

 48        }

 49
 50        /// <summary>
 51        /// 获取或设置行的指定列的数据
 52        /// </summary>
 53        /// <param name="columnName">列的名称</param>
 54        /// <returns>列中存储的数据</returns>

 55        public object this[string columnName]
 56        {
 57            set
 58            {
 59                this[colNames.IndexOf(columnName)] = value;
 60            }

 61            get
 62            {
 63                return this[colNames.IndexOf(columnName)];
 64            }

 65        }

 66        
 67
 68        /// <summary>
 69        /// 获取或设置行的全部数据
 70        /// </summary>

 71        public object[] ItemArray
 72        {
 73            get
 74            {
 75                return datas;
 76            }

 77            set
 78            {
 79                if (value.Length == datas.Length)
 80                {
 81                    datas = value; 
 82                    this.RowState = RowState.Modified;
 83                }

 84            }

 85        }

 86
 87        /// <summary>
 88        /// 将该行数据状态改为RowState.Unchanged
 89        /// </summary>

 90        public void AcceptChanges()
 91        {
 92            this.RowState = RowState.Unchanged;
 93        }

 94
 95        /// <summary>
 96        /// 为该行作删除标记
 97        /// </summary>

 98        public void Delete()
 99        {
100            this.RowState = RowState.Deleted;
101        }

102
103        /// <summary>
104        /// 将该行的状态设置为RowState.Added
105        /// </summary>

106        public void SetAdded()
107        {
108            if (this.RowState == RowState.Unchanged)
109            {
110                this.RowState = RowState.Added;
111            }

112        }

113
114        /// <summary>
115        /// 将该行数据状态设置为RowState.Modified
116        /// </summary>

117        public void SetModified()
118        {
119            if (this.RowState == RowState.Unchanged || this.RowState == RowState.Added)
120            {
121                this.RowState = RowState.Modified;
122            }

123        }

124    }


下一篇,我们定义Column和ColumnCollection
原文地址:https://www.cnblogs.com/shyleoking/p/649806.html