继上一节,回头看这张图片:
回顾上节的话题,怎么设计这样一个数据单元类?才不会有重复的单元表头,又能合理解释出数据与表头的关系?
经过长久的深思后。。一个关键的字出来了"ref",引用,是的,用的这就个,如果每个单元格,都包括值和单元表头,而单元表头,都引用同一个的时候,就刚好满足了需求。
于是,我们开始写出这样的类:
先构造出一个存放值的类:
/// <summary>
/// 只包函被填充的数据状态和值
/// </summary>
public class mdatacellvalue
{
/// <summary>
/// //值是否为空
/// </summary>
internal bool _isnull;
/// 值是否被改变了
internal bool _ischange;
/// 值是多少
internal object _value;
public mdatacellvalue()
{
_isnull = true;
_ischange = false;
_value = null;
}
}
接着,我们构造存放表头:
我们可以参考数据库,也可以参考datacell中的数据结构,构造出以下的类:
/// <summary>
/// 只包函数据库字段的属性
public class mdatacellstruct
internal bool _iscannull;
internal bool _isreadonly;
internal string _columnname;
internal system.data.sqldbtype _sqltype;
internal int _maxsize;
internal string _operator = "=";
internal parameterdirection _paradirection;
#region 构造函数
public mdatacellstruct(string columnname, system.data.sqldbtype sqltype, bool isreadonly, bool iscannull, int maxsize, parameterdirection paradirection)
_columnname = columnname;
_sqltype = sqltype;
_isreadonly = isreadonly;
_iscannull = iscannull;
_maxsize = maxsize;
_paradirection = paradirection;
#endregion
#region 属性
/// 数据字段列名称
public string columnname
get
{
return this._columnname;
}
/// 数据类型
public system.data.sqldbtype sqltype
return this._sqltype;
/// 数据字段列是否为只读
public bool isreadonly
return this._isreadonly;
/// 数据字段列长度大小
public int maxsize
return this._maxsize;
/// 数据字段列值是否能为空
public bool iscannull
return this._iscannull;
/// 存储过程时用的参数
public parameterdirection paradirection
return this._paradirection;
set
_paradirection = (parameterdirection)value;
public string operator
return _operator;
_operator = value;
紧跟着,我们要开始构造单元格了,它包含了数据单元结构和值两个类,同时,为了让以后所有行的单元格里的表头都指向同一个,我们用出了"ref"
public class mdatacell
internal mdatacellvalue _valuecontainer;
private mdatacellstruct _datastruct;
public mdatacell(ref mdatacellstruct datastruct)
init(ref datastruct, null);
public mdatacell(ref mdatacellstruct datastruct, object value)
init(ref datastruct, value);
#region 初始化
private void init(ref mdatacellstruct datastruct, object value)
_valuecontainer = new mdatacellvalue();
_datastruct = datastruct;
_valuecontainer._value = value;
为了方便从数据单元里访问数据结构和值,我们通过增加属性来对外开放
#region 属性
字段结构
/// 字段结构
public mdatacellstruct datastruct
return _datastruct;
/// 数据字段列值是否能改变
public bool ischange
return _valuecontainer._ischange;
_valuecontainer._ischange = value;
/// 数据字段列值是否为空
public bool isnull
return _valuecontainer._isnull;
/// 数据字段列值
public object value
return _valuecontainer._value;
if (_datastruct._iscannull)//数据库允许为null值
{
_valuecontainer._value = value;
_valuecontainer._ischange = true;
_valuecontainer._isnull = (value == null || value == dbnull.value);
}
else if (value != null && value != dbnull.value)//数据库不允许为null值不允许为null值
_valuecontainer._isnull = false;
#endregion
至此,我们终于构造完数据单元格,当然了,在对value的set属性中,以后我们会加上数据类型的比较和数据长度的验证,来增加数据的安全性
版权声明:本文原创发表于博客园,作者为路过秋天,原文链接:http://www.cnblogs.com/cyq1162/archive/2009/11/06/1597719.html