创建自定义数据源

看到一则使用CollectionBase为父类创建自定义数据源的例子:
None.gif using  System;
None.gif
namespace  自定义数据源
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif       
/**//// <summary>
InBlock.gif       
/// 自定义数据源
ExpandedSubBlockEnd.gif       
/// </summary>

InBlock.gif       public class cusdatasource : System.Collections.CollectionBase
ExpandedSubBlockStart.gifContractedSubBlock.gif       
dot.gif{
InBlock.gif
InBlock.gif                       
public cusdatasource()
ExpandedSubBlockStart.gifContractedSubBlock.gif                       
dot.gif{
InBlock.gif                               
for(int i = 0;i < 10;i++)
ExpandedSubBlockStart.gifContractedSubBlock.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.gifContractedSubBlock.gif       
dot.gif{
InBlock.gif               
private string name;
InBlock.gif               
public string ValueName
ExpandedSubBlockStart.gifContractedSubBlock.gif               
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                       
getdot.gif{return name;}
ExpandedSubBlockEnd.gif               }

InBlock.gif               
private int valu;
InBlock.gif               
public int Value
ExpandedSubBlockStart.gifContractedSubBlock.gif               
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                       
getdot.gif{return valu;}
ExpandedSubBlockEnd.gif               }

InBlock.gif               
public Element(int val,string nam)
ExpandedSubBlockStart.gifContractedSubBlock.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.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif       
/**//// <summary>
InBlock.gif       
/// source 的摘要说明。
ExpandedSubBlockEnd.gif       
/// </summary>

InBlock.gif       public class source:System.ComponentModel.IListSource
ExpandedSubBlockStart.gifContractedSubBlock.gif       
dot.gif{
InBlock.gif               
private data d=new data();
InBlock.gif               
public source()
ExpandedSubBlockStart.gifContractedSubBlock.gif               
dot.gif{
InBlock.gif                       
for(int i=0;i<10;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                       
dot.gif
InBlock.gif                               d.Add(
new dataitem(i,string.Format("this is {0}",i)));
ExpandedSubBlockEnd.gif                       }

ExpandedSubBlockEnd.gif               }

ContractedSubBlock.gifExpandedSubBlockStart.gif               
IListSource 成员#region IListSource 成员
InBlock.gif
InBlock.gif               
public System.Collections.IList GetList()
ExpandedSubBlockStart.gifContractedSubBlock.gif               
dot.gif{
InBlock.gif                       
// TODO:  添加 source.GetList 实现
InBlock.gif
                       return d;
ExpandedSubBlockEnd.gif               }

InBlock.gif
InBlock.gif               
public bool ContainsListCollection
ExpandedSubBlockStart.gifContractedSubBlock.gif               
dot.gif{
InBlock.gif                       
get
ExpandedSubBlockStart.gifContractedSubBlock.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.gifContractedSubBlock.gif       
dot.gif{
InBlock.gif               
protected System.Collections.ArrayList _dataitems;
InBlock.gif               
protected int _ptr=0;
InBlock.gif               
public data()
ExpandedSubBlockStart.gifContractedSubBlock.gif               
dot.gif{
InBlock.gif                       _dataitems
=new System.Collections.ArrayList();
ExpandedSubBlockEnd.gif               }

ContractedSubBlock.gifExpandedSubBlockStart.gif               
IList 成员#region IList 成员
InBlock.gif
InBlock.gif               
public bool IsReadOnly
ExpandedSubBlockStart.gifContractedSubBlock.gif               
dot.gif{
InBlock.gif                       
get
ExpandedSubBlockStart.gifContractedSubBlock.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.gifContractedSubBlock.gif               
dot.gif{
InBlock.gif                       
get
ExpandedSubBlockStart.gifContractedSubBlock.gif                       
dot.gif{
InBlock.gif
InBlock.gif                               
return _dataitems[index];
ExpandedSubBlockEnd.gif                       }

InBlock.gif                       
set
ExpandedSubBlockStart.gifContractedSubBlock.gif                       
dot.gif{
InBlock.gif                               _dataitems[index]
=value;
ExpandedSubBlockEnd.gif                       }

ExpandedSubBlockEnd.gif               }

InBlock.gif
InBlock.gif               
public void RemoveAt(int index)
ExpandedSubBlockStart.gifContractedSubBlock.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.gifContractedSubBlock.gif               
dot.gif{
InBlock.gif                       
if(index>=0 && index<_dataitems.Count)
ExpandedSubBlockStart.gifContractedSubBlock.gif                       
dot.gif{
InBlock.gif                               _dataitems.Insert(index,value);
ExpandedSubBlockEnd.gif                       }

ExpandedSubBlockEnd.gif               }

InBlock.gif
InBlock.gif               
public void Remove(object value)
ExpandedSubBlockStart.gifContractedSubBlock.gif               
dot.gif{
InBlock.gif                       _dataitems.Remove(value);
ExpandedSubBlockEnd.gif               }

InBlock.gif
InBlock.gif               
public bool Contains(object value)
ExpandedSubBlockStart.gifContractedSubBlock.gif               
dot.gif{
InBlock.gif                       
return _dataitems.Contains(value);
ExpandedSubBlockEnd.gif               }

InBlock.gif
InBlock.gif               
public void Clear()
ExpandedSubBlockStart.gifContractedSubBlock.gif               
dot.gif{
InBlock.gif                       _dataitems.Clear();
ExpandedSubBlockEnd.gif               }

InBlock.gif
InBlock.gif               
public int IndexOf(object value)
ExpandedSubBlockStart.gifContractedSubBlock.gif               
dot.gif{
InBlock.gif                       
return _dataitems.IndexOf(value);
ExpandedSubBlockEnd.gif               }

InBlock.gif
InBlock.gif               
public int Add(object value)
ExpandedSubBlockStart.gifContractedSubBlock.gif               
dot.gif{
InBlock.gif                       
return _dataitems.Add(value);
ExpandedSubBlockEnd.gif               }

InBlock.gif
InBlock.gif               
public bool IsFixedSize
ExpandedSubBlockStart.gifContractedSubBlock.gif               
dot.gif{
InBlock.gif                       
get
ExpandedSubBlockStart.gifContractedSubBlock.gif                       
dot.gif{
InBlock.gif                               
return _dataitems.IsFixedSize;
ExpandedSubBlockEnd.gif                       }

ExpandedSubBlockEnd.gif               }

InBlock.gif
ExpandedSubBlockEnd.gif               
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif               
ICollection 成员#region ICollection 成员
InBlock.gif
InBlock.gif               
public bool IsSynchronized
ExpandedSubBlockStart.gifContractedSubBlock.gif               
dot.gif{
InBlock.gif                       
get
ExpandedSubBlockStart.gifContractedSubBlock.gif                       
dot.gif{
InBlock.gif                               
return false;
ExpandedSubBlockEnd.gif                       }

ExpandedSubBlockEnd.gif               }

InBlock.gif
InBlock.gif               
public int Count
ExpandedSubBlockStart.gifContractedSubBlock.gif               
dot.gif{
InBlock.gif                       
get
ExpandedSubBlockStart.gifContractedSubBlock.gif                       
dot.gif{
InBlock.gif                             
return _dataitems.Count;
ExpandedSubBlockEnd.gif                       }

ExpandedSubBlockEnd.gif               }

InBlock.gif
InBlock.gif               
public void CopyTo(Array array, int index)
ExpandedSubBlockStart.gifContractedSubBlock.gif               
dot.gif{
ExpandedSubBlockEnd.gif               }

InBlock.gif
InBlock.gif               
public object SyncRoot
ExpandedSubBlockStart.gifContractedSubBlock.gif               
dot.gif{
InBlock.gif                       
get
ExpandedSubBlockStart.gifContractedSubBlock.gif                       
dot.gif{
InBlock.gif                               
return null;
ExpandedSubBlockEnd.gif                       }

ExpandedSubBlockEnd.gif               }

InBlock.gif
ExpandedSubBlockEnd.gif               
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif               
IEnumerable 成员#region IEnumerable 成员
InBlock.gif
InBlock.gif               
public System.Collections.IEnumerator GetEnumerator()
ExpandedSubBlockStart.gifContractedSubBlock.gif               
dot.gif{
InBlock.gif                       
return this;
ExpandedSubBlockEnd.gif               }

InBlock.gif
ExpandedSubBlockEnd.gif               
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif               
IEnumerator 成员#region IEnumerator 成员
InBlock.gif
InBlock.gif               
public void Reset()
ExpandedSubBlockStart.gifContractedSubBlock.gif               
dot.gif{
InBlock.gif                       _ptr
=0;
ExpandedSubBlockEnd.gif               }

InBlock.gif
InBlock.gif               
public object Current
ExpandedSubBlockStart.gifContractedSubBlock.gif               
dot.gif{
InBlock.gif                       
get
ExpandedSubBlockStart.gifContractedSubBlock.gif                       
dot.gif{
InBlock.gif                               
return this[_ptr++];
ExpandedSubBlockEnd.gif                       }

ExpandedSubBlockEnd.gif               }

InBlock.gif
InBlock.gif               
public bool MoveNext()
ExpandedSubBlockStart.gifContractedSubBlock.gif               
dot.gif{
InBlock.gif                       
if(_ptr<this.Count)
ExpandedSubBlockStart.gifContractedSubBlock.gif                       
dot.gif{
InBlock.gif                               
return true;
ExpandedSubBlockEnd.gif                       }

InBlock.gif                       
else
ExpandedSubBlockStart.gifContractedSubBlock.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.gifContractedSubBlock.gif       
dot.gif{
InBlock.gif               
private string name;
InBlock.gif               
public string ValueName
ExpandedSubBlockStart.gifContractedSubBlock.gif               
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                       
getdot.gif{return name;}
ExpandedSubBlockEnd.gif               }

InBlock.gif               
private int valu;
InBlock.gif               
public int Value
ExpandedSubBlockStart.gifContractedSubBlock.gif               
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                       
getdot.gif{return valu;}
ExpandedSubBlockEnd.gif               }
 
InBlock.gif               
public dataitem(int val,string nam)
ExpandedSubBlockStart.gifContractedSubBlock.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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值