Table是怎样炼成的:Row、RowCollection

本文介绍了一个用于管理二维表数据行的C#类,包括数据行的状态定义、数据的存取方法及状态变更操作。
接下来的连续几篇,我们要演练作一个描述通用的二维表,并演示该二维表的继承通途。

先定义数据行的状态


1ExpandedBlockStart.gifContractedBlock.gif/**////<summary>
2InBlock.gif///数据行的状态
3ExpandedBlockEnd.gif///</summary>

4None.gifpublicenumRowState
5ExpandedBlockStart.gifContractedBlock.gifdot.gif{
6ExpandedSubBlockStart.gifContractedSubBlock.gif/**////<summary>
7InBlock.gif///新建状态
8ExpandedSubBlockEnd.gif///</summary>

9InBlock.gifAdded,
10ExpandedSubBlockStart.gifContractedSubBlock.gif/**////<summary>
11InBlock.gif///删除状态
12ExpandedSubBlockEnd.gif///</summary>

13InBlock.gifDeleted,
14ExpandedSubBlockStart.gifContractedSubBlock.gif/**////<summary>
15InBlock.gif///修改状态
16ExpandedSubBlockEnd.gif///</summary>

17InBlock.gifModified,
18ExpandedSubBlockStart.gifContractedSubBlock.gif/**////<summary>
19InBlock.gif///为止状态
20ExpandedSubBlockEnd.gif///</summary>

21InBlock.gifUnchanged
22ExpandedBlockEnd.gif}

开始定义数据行了

1ExpandedBlockStart.gifContractedBlock.gif/**////<summary>
2InBlock.gif///数据行
3ExpandedBlockEnd.gif///</summary>

4None.gifpublicclassRow
5ExpandedBlockStart.gifContractedBlock.gifdot.gif{
6InBlock.gifprivateobject[]datas;
7InBlock.gifprivateSystem.Collections.ArrayListcolNames;
8InBlock.gif
9ExpandedSubBlockStart.gifContractedSubBlock.gif/**////<summary>
10InBlock.gif///该数据行所依赖的表的名称
11ExpandedSubBlockEnd.gif///</summary>

12InBlock.gifpublicreadonlystringTableName;
13ExpandedSubBlockStart.gifContractedSubBlock.gif/**////<summary>
14InBlock.gif///该数据行的状态
15ExpandedSubBlockEnd.gif///</summary>

16InBlock.gifpublicRowStateRowState;
17InBlock.gif
18InBlock.gif
19InBlock.gifprotectedinternalRow(ColumnCollectioncolumns,stringtableName)
20ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
21InBlock.gifdatas=newobject[columns.Count];
22InBlock.gifthis.TableName=tableName;
23InBlock.gifcolNames=newSystem.Collections.ArrayList();
24InBlock.gifforeach(Columncolincolumns)
25ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
26InBlock.gifcolNames.Add(col.ColumnName);
27ExpandedSubBlockEnd.gif}

28ExpandedSubBlockEnd.gif}

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

37InBlock.gifpublicobjectthis[intindex]
38ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
39InBlock.gifset
40ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
41InBlock.gifdatas[index]=value;
42InBlock.gifthis.RowState=RowState.Modified;
43ExpandedSubBlockEnd.gif}

44InBlock.gifget
45ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
46InBlock.gifreturndatas[index];
47ExpandedSubBlockEnd.gif}

48ExpandedSubBlockEnd.gif}

49InBlock.gif
50ExpandedSubBlockStart.gifContractedSubBlock.gif/**////<summary>
51InBlock.gif///获取或设置行的指定列的数据
52InBlock.gif///</summary>
53InBlock.gif///<paramname="columnName">列的名称</param>
54ExpandedSubBlockEnd.gif///<returns>列中存储的数据</returns>

55InBlock.gifpublicobjectthis[stringcolumnName]
56ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
57InBlock.gifset
58ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
59InBlock.gifthis[colNames.IndexOf(columnName)]=value;
60ExpandedSubBlockEnd.gif}

61InBlock.gifget
62ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
63InBlock.gifreturnthis[colNames.IndexOf(columnName)];
64ExpandedSubBlockEnd.gif}

65ExpandedSubBlockEnd.gif}

66InBlock.gif
67InBlock.gif
68ExpandedSubBlockStart.gifContractedSubBlock.gif/**////<summary>
69InBlock.gif///获取或设置行的全部数据
70ExpandedSubBlockEnd.gif///</summary>

71InBlock.gifpublicobject[]ItemArray
72ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
73InBlock.gifget
74ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
75InBlock.gifreturndatas;
76ExpandedSubBlockEnd.gif}

77InBlock.gifset
78ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
79InBlock.gifif(value.Length==datas.Length)
80ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
81InBlock.gifdatas=value;
82InBlock.gifthis.RowState=RowState.Modified;
83ExpandedSubBlockEnd.gif}

84ExpandedSubBlockEnd.gif}

85ExpandedSubBlockEnd.gif}

86InBlock.gif
87ExpandedSubBlockStart.gifContractedSubBlock.gif/**////<summary>
88InBlock.gif///将该行数据状态改为RowState.Unchanged
89ExpandedSubBlockEnd.gif///</summary>

90InBlock.gifpublicvoidAcceptChanges()
91ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
92InBlock.gifthis.RowState=RowState.Unchanged;
93ExpandedSubBlockEnd.gif}

94InBlock.gif
95ExpandedSubBlockStart.gifContractedSubBlock.gif/**////<summary>
96InBlock.gif///为该行作删除标记
97ExpandedSubBlockEnd.gif///</summary>

98InBlock.gifpublicvoidDelete()
99ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
100InBlock.gifthis.RowState=RowState.Deleted;
101ExpandedSubBlockEnd.gif}

102InBlock.gif
103ExpandedSubBlockStart.gifContractedSubBlock.gif/**////<summary>
104InBlock.gif///将该行的状态设置为RowState.Added
105ExpandedSubBlockEnd.gif///</summary>

106InBlock.gifpublicvoidSetAdded()
107ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
108InBlock.gifif(this.RowState==RowState.Unchanged)
109ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
110InBlock.gifthis.RowState=RowState.Added;
111ExpandedSubBlockEnd.gif}

112ExpandedSubBlockEnd.gif}

113InBlock.gif
114ExpandedSubBlockStart.gifContractedSubBlock.gif/**////<summary>
115InBlock.gif///将该行数据状态设置为RowState.Modified
116ExpandedSubBlockEnd.gif///</summary>

117InBlock.gifpublicvoidSetModified()
118ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
119InBlock.gifif(this.RowState==RowState.Unchanged||this.RowState==RowState.Added)
120ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
121InBlock.gifthis.RowState=RowState.Modified;
122ExpandedSubBlockEnd.gif}

123ExpandedSubBlockEnd.gif}

124ExpandedBlockEnd.gif}


下一篇,我们定义Column和ColumnCollection
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值