博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
创建自定义数据源
阅读量:6951 次
发布时间:2019-06-27

本文共 3287 字,大约阅读时间需要 10 分钟。

看到一则使用CollectionBase为父类创建自定义数据源的例子:
None.gif
using System;
None.gif
namespace 自定义数据源
ExpandedBlockStart.gif
ContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gif
ContractedSubBlock.gif       
/**/
///
 
<summary>
InBlock.gif       
///
 自定义数据源
ExpandedSubBlockEnd.gif       
///
 
</summary>
InBlock.gif       
public 
class cusdatasource : System.Collections.CollectionBase
ExpandedSubBlockStart.gif
ContractedSubBlock.gif       
dot.gif{
InBlock.gif
InBlock.gif                       
public cusdatasource()
ExpandedSubBlockStart.gif
ContractedSubBlock.gif                       
dot.gif{
InBlock.gif                               
for(
int i = 0;i < 10;i++)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif                               
dot.gif{
InBlock.gif                                       
base.InnerList.Add(
new Element(i,
string.Format("a[{0}]",i)));
ExpandedSubBlockEnd.gif                               }
ExpandedSubBlockEnd.gif                       }
ExpandedSubBlockEnd.gif       }
InBlock.gif
InBlock.gif       
public 
class Element
ExpandedSubBlockStart.gif
ContractedSubBlock.gif       
dot.gif{
InBlock.gif               
private 
string name;
InBlock.gif               
public 
string ValueName
ExpandedSubBlockStart.gif
ContractedSubBlock.gif               
dot.gif{
ExpandedSubBlockStart.gif
ContractedSubBlock.gif                       
get
dot.gif{
return name;}
ExpandedSubBlockEnd.gif               }
InBlock.gif               
private 
int valu;
InBlock.gif               
public 
int Value
ExpandedSubBlockStart.gif
ContractedSubBlock.gif               
dot.gif{
ExpandedSubBlockStart.gif
ContractedSubBlock.gif                       
get
dot.gif{
return valu;}
ExpandedSubBlockEnd.gif               }
InBlock.gif               
public Element(
int val,
string nam)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif               
dot.gif{
InBlock.gif                       name = nam;
InBlock.gif                       valu = val;
ExpandedSubBlockEnd.gif               }
ExpandedSubBlockEnd.gif       }
ExpandedBlockEnd.gif}
然后我们new一个cusdatasource,并绑定到datagrid上就会出现2列:value和name;
我们还可以通过实现IListSource 或 IEnumerable 接口,来制作自定义的数据源,较上面的麻烦一点,不过更灵活:
None.gif
using System;
None.gif
None.gif
namespace personaltest
ExpandedBlockStart.gif
ContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gif
ContractedSubBlock.gif       
/**/
///
 
<summary>
InBlock.gif       
///
 source 的摘要说明。
ExpandedSubBlockEnd.gif       
///
 
</summary>
InBlock.gif       
public 
class source:System.ComponentModel.IListSource
ExpandedSubBlockStart.gif
ContractedSubBlock.gif       
dot.gif{
InBlock.gif               
private data d=
new data();
InBlock.gif               
public source()
ExpandedSubBlockStart.gif
ContractedSubBlock.gif               
dot.gif{
InBlock.gif                       
for(
int i=0;i<10;i++)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif                       
dot.gif
InBlock.gif                               d.Add(
new dataitem(i,
string.Format("this is {0}",i)));
ExpandedSubBlockEnd.gif                       }
ExpandedSubBlockEnd.gif               }
ContractedSubBlock.gif
ExpandedSubBlockStart.gif               
IListSource 成员
#region IListSource 成员
InBlock.gif
InBlock.gif               
public System.Collections.IList GetList()
ExpandedSubBlockStart.gif
ContractedSubBlock.gif               
dot.gif{
InBlock.gif                       
//
 TODO:  添加 source.GetList 实现
InBlock.gif
                       
return d;
ExpandedSubBlockEnd.gif               }
InBlock.gif
InBlock.gif               
public 
bool ContainsListCollection
ExpandedSubBlockStart.gif
ContractedSubBlock.gif               
dot.gif{
InBlock.gif                       
get
ExpandedSubBlockStart.gif
ContractedSubBlock.gif                       
dot.gif{
InBlock.gif                               
//
 TODO:  添加 source.ContainsListCollection getter 实现
InBlock.gif
                               
return 
false;
ExpandedSubBlockEnd.gif                       }
ExpandedSubBlockEnd.gif               }
InBlock.gif
ExpandedSubBlockEnd.gif               
#endregion
ExpandedSubBlockEnd.gif       }
InBlock.gif
InBlock.gif       
public 
class data:System.Collections.IList,System.Collections.IEnumerator
ExpandedSubBlockStart.gif
ContractedSubBlock.gif       
dot.gif{
InBlock.gif               
protected System.Collections.ArrayList _dataitems;
InBlock.gif               
protected 
int _ptr=0;
InBlock.gif               
public data()
ExpandedSubBlockStart.gif
ContractedSubBlock.gif               
dot.gif{
InBlock.gif                       _dataitems=
new System.Collections.ArrayList();
ExpandedSubBlockEnd.gif               }
ContractedSubBlock.gif
ExpandedSubBlockStart.gif               
IList 成员
#region IList 成员
InBlock.gif
InBlock.gif               
public 
bool IsReadOnly
ExpandedSubBlockStart.gif
ContractedSubBlock.gif               
dot.gif{
InBlock.gif                       
get
ExpandedSubBlockStart.gif
ContractedSubBlock.gif                       
dot.gif{
InBlock.gif                               
//
 TODO:  添加 data.IsReadOnly getter 实现
InBlock.gif
                               
return 
false;
ExpandedSubBlockEnd.gif                       }
ExpandedSubBlockEnd.gif               }
InBlock.gif
InBlock.gif               
public 
object 
this[
int index]
ExpandedSubBlockStart.gif
ContractedSubBlock.gif               
dot.gif{
InBlock.gif                       
get
ExpandedSubBlockStart.gif
ContractedSubBlock.gif                       
dot.gif{
InBlock.gif
InBlock.gif                               
return _dataitems[index];
ExpandedSubBlockEnd.gif                       }
InBlock.gif                       
set
ExpandedSubBlockStart.gif
ContractedSubBlock.gif                       
dot.gif{
InBlock.gif                               _dataitems[index]=value;
ExpandedSubBlockEnd.gif                       }
ExpandedSubBlockEnd.gif               }
InBlock.gif
InBlock.gif               
public 
void RemoveAt(
int index)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif               
dot.gif{
InBlock.gif                       
if(index>=0 && index<_dataitems.Count)
InBlock.gif                               _dataitems.RemoveAt(index);
ExpandedSubBlockEnd.gif               }
InBlock.gif
InBlock.gif               
public 
void Insert(
int index, 
object value)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif               
dot.gif{
InBlock.gif                       
if(index>=0 && index<_dataitems.Count)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif                       
dot.gif{
InBlock.gif                               _dataitems.Insert(index,value);
ExpandedSubBlockEnd.gif                       }
ExpandedSubBlockEnd.gif               }
InBlock.gif
InBlock.gif               
public 
void Remove(
object value)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif               
dot.gif{
InBlock.gif                       _dataitems.Remove(value);
ExpandedSubBlockEnd.gif               }
InBlock.gif
InBlock.gif               
public 
bool Contains(
object value)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif               
dot.gif{
InBlock.gif                       
return _dataitems.Contains(value);
ExpandedSubBlockEnd.gif               }
InBlock.gif
InBlock.gif               
public 
void Clear()
ExpandedSubBlockStart.gif
ContractedSubBlock.gif               
dot.gif{
InBlock.gif                       _dataitems.Clear();
ExpandedSubBlockEnd.gif               }
InBlock.gif
InBlock.gif               
public 
int IndexOf(
object value)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif               
dot.gif{
InBlock.gif                       
return _dataitems.IndexOf(value);
ExpandedSubBlockEnd.gif               }
InBlock.gif
InBlock.gif               
public 
int Add(
object value)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif               
dot.gif{
InBlock.gif                       
return _dataitems.Add(value);
ExpandedSubBlockEnd.gif               }
InBlock.gif
InBlock.gif               
public 
bool IsFixedSize
ExpandedSubBlockStart.gif
ContractedSubBlock.gif               
dot.gif{
InBlock.gif                       
get
ExpandedSubBlockStart.gif
ContractedSubBlock.gif                       
dot.gif{
InBlock.gif                               
return _dataitems.IsFixedSize;
ExpandedSubBlockEnd.gif                       }
ExpandedSubBlockEnd.gif               }
InBlock.gif
ExpandedSubBlockEnd.gif               
#endregion
InBlock.gif
ContractedSubBlock.gif
ExpandedSubBlockStart.gif               
ICollection 成员
#region ICollection 成员
InBlock.gif
InBlock.gif               
public 
bool IsSynchronized
ExpandedSubBlockStart.gif
ContractedSubBlock.gif               
dot.gif{
InBlock.gif                       
get
ExpandedSubBlockStart.gif
ContractedSubBlock.gif                       
dot.gif{
InBlock.gif                               
return 
false;
ExpandedSubBlockEnd.gif                       }
ExpandedSubBlockEnd.gif               }
InBlock.gif
InBlock.gif               
public 
int Count
ExpandedSubBlockStart.gif
ContractedSubBlock.gif               
dot.gif{
InBlock.gif                       
get
ExpandedSubBlockStart.gif
ContractedSubBlock.gif                       
dot.gif{
InBlock.gif                             
return _dataitems.Count;
ExpandedSubBlockEnd.gif                       }
ExpandedSubBlockEnd.gif               }
InBlock.gif
InBlock.gif               
public 
void CopyTo(Array array, 
int index)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif               
dot.gif{
ExpandedSubBlockEnd.gif               }
InBlock.gif
InBlock.gif               
public 
object SyncRoot
ExpandedSubBlockStart.gif
ContractedSubBlock.gif               
dot.gif{
InBlock.gif                       
get
ExpandedSubBlockStart.gif
ContractedSubBlock.gif                       
dot.gif{
InBlock.gif                               
return 
null;
ExpandedSubBlockEnd.gif                       }
ExpandedSubBlockEnd.gif               }
InBlock.gif
ExpandedSubBlockEnd.gif               
#endregion
InBlock.gif
ContractedSubBlock.gif
ExpandedSubBlockStart.gif               
IEnumerable 成员
#region IEnumerable 成员
InBlock.gif
InBlock.gif               
public System.Collections.IEnumerator GetEnumerator()
ExpandedSubBlockStart.gif
ContractedSubBlock.gif               
dot.gif{
InBlock.gif                       
return 
this;
ExpandedSubBlockEnd.gif               }
InBlock.gif
ExpandedSubBlockEnd.gif               
#endregion
InBlock.gif
ContractedSubBlock.gif
ExpandedSubBlockStart.gif               
IEnumerator 成员
#region IEnumerator 成员
InBlock.gif
InBlock.gif               
public 
void Reset()
ExpandedSubBlockStart.gif
ContractedSubBlock.gif               
dot.gif{
InBlock.gif                       _ptr=0;
ExpandedSubBlockEnd.gif               }
InBlock.gif
InBlock.gif               
public 
object Current
ExpandedSubBlockStart.gif
ContractedSubBlock.gif               
dot.gif{
InBlock.gif                       
get
ExpandedSubBlockStart.gif
ContractedSubBlock.gif                       
dot.gif{
InBlock.gif                               
return 
this[_ptr++];
ExpandedSubBlockEnd.gif                       }
ExpandedSubBlockEnd.gif               }
InBlock.gif
InBlock.gif               
public 
bool MoveNext()
ExpandedSubBlockStart.gif
ContractedSubBlock.gif               
dot.gif{
InBlock.gif                       
if(_ptr<
this.Count)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif                       
dot.gif{
InBlock.gif                               
return 
true;
ExpandedSubBlockEnd.gif                       }
InBlock.gif                       
else
ExpandedSubBlockStart.gif
ContractedSubBlock.gif                       
dot.gif{
InBlock.gif                               
this.Reset();
InBlock.gif                               
return 
false;
ExpandedSubBlockEnd.gif                       }
ExpandedSubBlockEnd.gif               }
InBlock.gif
ExpandedSubBlockEnd.gif               
#endregion
ExpandedSubBlockEnd.gif       }
InBlock.gif
InBlock.gif       
public 
class dataitem
ExpandedSubBlockStart.gif
ContractedSubBlock.gif       
dot.gif{
InBlock.gif               
private 
string name;
InBlock.gif               
public 
string ValueName
ExpandedSubBlockStart.gif
ContractedSubBlock.gif               
dot.gif{
ExpandedSubBlockStart.gif
ContractedSubBlock.gif                       
get
dot.gif{
return name;}
ExpandedSubBlockEnd.gif               }
InBlock.gif               
private 
int valu;
InBlock.gif               
public 
int Value
ExpandedSubBlockStart.gif
ContractedSubBlock.gif               
dot.gif{
ExpandedSubBlockStart.gif
ContractedSubBlock.gif                       
get
dot.gif{
return valu;}
ExpandedSubBlockEnd.gif               } 
InBlock.gif               
public dataitem(
int val,
string nam)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif               
dot.gif{
InBlock.gif                       name = nam;
InBlock.gif                       valu = val;
ExpandedSubBlockEnd.gif               }
ExpandedSubBlockEnd.gif       }
ExpandedBlockEnd.gif}
None.gif
实现了IListSource接口的自定义数据源,IEnumerable在其中也有实现;
需要注意的地方,IEnumerator接口的movenext()方法,在foreach语句的时候会先执行一次,然后才会用current()方法返回"当前值",所以指针初始化为0
的话就不能在movenext()方法中递增指针,而应该放在current()中。
引用:http://www.cnblogs.com/zpisgod/articles/70024.html
你可能感兴趣的文章
MySQL Study之--MySQL下图形工具的使用(phpMyAdmin)
查看>>
ASP.NET MVC4 乱七八糟罗列
查看>>
java面试
查看>>
SpringMVC中JSP页面不显示EL表达式的原因
查看>>
每周百万封业务邮件的服务器不知道为啥就down掉了?
查看>>
用Windows XP系统安装声卡驱动程序 (UAA)
查看>>
centos-5.5安装vmvare tools
查看>>
asp.net 调用echarts显示图表控件随浏览器自适应解决方案
查看>>
Oracle 备份与恢复学习笔记(8)
查看>>
初识 Powershell 5.0 class
查看>>
Dubbo点滴(1) SPI入门
查看>>
WebSocket+HTML5实现在线聊天室
查看>>
nagios扩展开发之check_ping
查看>>
控制文件丢失的恢复(续)
查看>>
in-band(带内) and out-of-band(带外) management
查看>>
db link的查看创建与删除
查看>>
Android小项目合集(经典教程)包含十五个Android开发应用实例
查看>>
利用EEPROM实现arduino的断电存储
查看>>
如何查询redhat的版本信息
查看>>
kvm虚拟化管理平台WebVirtMgr部署-完整记录(安装Windows虚拟机)-(4)
查看>>