本章起,將續章講解整框架當初的設計思路:
本章既為續章,說明我以前寫過,是的,以前我寫過內部整個MDataTable的構造,不過,當初匆匆寫完后,
最后一步的實現MDataTable綁定GridView/DataList/Repeater還差一點,這章續上!
這里列出我以前寫過的關于構造自定義MDataTable系列文章:
備注:以下內容為早期所寫,文字少,代碼多,有不明之處,歡迎在文章后面留言!
?
1:CYQ.Data 輕量數據訪問層(二) 構造數據單元(上)
2:CYQ.Data 輕量數據訪問層(三) 構造數據單元(下)
3:CYQ.Data 輕量數據訪問層(四) 構造數據單元列
4:CYQ.Data 輕量數據訪問層(五) 構造數據行
5:CYQ.Data 輕量數據訪問層(六) 構造數據表
6:CYQ.Data 輕量數據訪問層(七) 自定義數據表實現綁定常用的數據控件(上)
7:CYQ.Data 輕量數據訪問層(八) 自定義數據表實現綁定常用的數據控件(中)
8:CYQ.Data 輕量數據訪問層(九) 自定義數據表實現綁定常用的數據控件(下)
?
在寫完第八篇(九)之后,我們的測試結果里,并沒有完成綁定功能,我們來看一下測試代碼:

????????????table.Columns.Add("Url",?SqlDbType.NVarChar);
????????????table.Columns.Add("Name",SqlDbType.NVarChar);
????????????MDataRow?mdr?=?table.NewRow();
????????????mdr[0].Value?=?"http://cyq1162.cnblogs.com/";
????????????mdr[1].Value?=?"路過秋天";
????????????table.Rows.Add(mdr);
????????????GridView1.DataSource?=?table;
????????????GridView1.DataBind();
?
我們像普通的DataTable一樣,添加了兩列,然后對列賦值:
我們看一下測試的結果:
很明顯,綁定的結果亂七雜八,不是我們想要的。?
?
經過代碼對比,發現,我們的MDataRow得實現IDataRecord接口才行,于是,讓IDataRecord繼承接口,并實現:

????{
???????///?...省略N行已有代碼...
????????#region?IDataRecord?成員
????????int?IDataRecord.FieldCount
????????{
????????????get
????????????{
????????????????return?base.Count;
????????????}
????????}
????????bool?IDataRecord.GetBoolean(int?i)
????????{
????????????return?(bool)this[i].Value;
????????}
????????byte?IDataRecord.GetByte(int?i)
????????{
????????????return?(byte)this[i].Value;
????????}
????????long?IDataRecord.GetBytes(int?i,?long?fieldOffset,?byte[]?buffer,?int?bufferoffset,?int?length)
????????{
????????????throw?new?Exception("The?method?or?operation?is?not?implemented.");
????????}
????????char?IDataRecord.GetChar(int?i)
????????{
????????????return?(char)this[i].Value;
????????}
????????long?IDataRecord.GetChars(int?i,?long?fieldoffset,?char[]?buffer,?int?bufferoffset,?int?length)
????????{
????????????return?(long)this[i].Value;
????????}
????????IDataReader?IDataRecord.GetData(int?i)
????????{
????????????throw?new?Exception("The?method?or?operation?is?not?implemented.");
????????}
????????string?IDataRecord.GetDataTypeName(int?i)
????????{
????????????return?(string)this[i].Value;
????????}
????????DateTime?IDataRecord.GetDateTime(int?i)
????????{
????????????return?(DateTime)this[i].Value;
????????}
????????decimal?IDataRecord.GetDecimal(int?i)
????????{
????????????return?(decimal)this[i].Value;
????????}
????????double?IDataRecord.GetDouble(int?i)
????????{
????????????return?(double)this[i].Value;
????????}
????????Type?IDataRecord.GetFieldType(int?i)
????????{
????????????return?this[i].Value.GetType();
????????}
????????float?IDataRecord.GetFloat(int?i)
????????{
????????????return?(float)this[i].Value;
????????}
????????Guid?IDataRecord.GetGuid(int?i)
????????{
????????????return?(Guid)this[i].Value;
????????}
????????short?IDataRecord.GetInt16(int?i)
????????{
????????????return?(short)this[i].Value;
????????}
????????int?IDataRecord.GetInt32(int?i)
????????{
????????????return?(int)this[i].Value;
????????}
????????long?IDataRecord.GetInt64(int?i)
????????{
????????????return?(long)this[i].Value;
????????}
????????string?IDataRecord.GetName(int?i)
????????{
????????????return?(string)this[i].Value;
????????}
????????int?IDataRecord.GetOrdinal(string?name)
????????{
????????????return?(int)this[name].Value;
????????}
????????string?IDataRecord.GetString(int?i)
????????{
????????????return?(string)this[i].Value;
????????}
????????object?IDataRecord.GetValue(int?i)
????????{
????????????return?this[i].Value;
????????}
????????int?IDataRecord.GetValues(object[]?values)
????????{
????????????return?0;
????????}
????????bool?IDataRecord.IsDBNull(int?i)
????????{
????????????return?this[i].Value?==?DBNull.Value;
????????}
????????object?IDataRecord.this[string?name]
????????{
????????????get
????????????{
????????????????return?this[name].Value;
????????????}
????????}
????????object?IDataRecord.this[int?i]
????????{
????????????get
????????????{
????????????????return?this[i].Value;
????????????}
????????}
????????#endregion
????}
?
接著瀏覽了一下,不見啥效果。
?
于是又對比了一下代碼,發現原來的MDataTable是采用繼承方式List<MDataRow>,
于是,把它給弄到下面來了:

????{
????????private?List<MDataRow>?_Mdr;
????????public?List<MDataRow>?Rows
????????{
????????????get
????????????{
????????????????return?_Mdr;
????????????}
????????}
?????//...下面省略N行...
}
?
?
接著小調整了一下,再次瀏覽,終于效果出來了:
?
?
?
至此,整個框架三部分中自定義MDataTable系列,就到此結束了。
?
?
備注:完整框架源碼會在本系列結束之后另開章節發布,暫時望勿激動,學習思想才是重要的。
如果在學習過程中發現有什么問題,歡迎留言!
版權聲明:本文原創發表于博客園,作者為路過秋天,原文鏈接:
http://www.cnblogs.com/cyq1162/archive/2010/08/24/1806300.html