using
System;
namespace 自定义数据源
{
/**//// <summary>
/// 自定义数据源
/// </summary>
public class cusdatasource : System.Collections.CollectionBase
{
public cusdatasource()
{
for(int i = 0;i < 10;i++)
{
base.InnerList.Add(new Element(i,string.Format("a[{0}]",i)));
}
}
}
public class Element
{
private string name;
public string ValueName
{
get{return name;}
}
private int valu;
public int Value
{
get{return valu;}
}
public Element(int val,string nam)
{
name = nam;
valu = val;
}
}
}
namespace 自定义数据源
{
/**//// <summary>
/// 自定义数据源
/// </summary>
public class cusdatasource : System.Collections.CollectionBase
{
public cusdatasource()
{
for(int i = 0;i < 10;i++)
{
base.InnerList.Add(new Element(i,string.Format("a[{0}]",i)));
}
}
}
public class Element
{
private string name;
public string ValueName
{
get{return name;}
}
private int valu;
public int Value
{
get{return valu;}
}
public Element(int val,string nam)
{
name = nam;
valu = val;
}
}
}
然后我们new一个cusdatasource,并绑定到datagrid上就会出现2列:value和name;
我们还可以通过实现IListSource 或 IEnumerable 接口,来制作自定义的数据源,较上面的麻烦一点,不过更灵活:
using
System;
namespace personaltest
{
/**//// <summary>
/// source 的摘要说明。
/// </summary>
public class source:System.ComponentModel.IListSource
{
private data d=new data();
public source()
{
for(int i=0;i<10;i++)
{
d.Add(new dataitem(i,string.Format("this is {0}",i)));
}
}
IListSource 成员#region IListSource 成员
public System.Collections.IList GetList()
{
// TODO: 添加 source.GetList 实现
return d;
}
public bool ContainsListCollection
{
get
{
// TODO: 添加 source.ContainsListCollection getter 实现
return false;
}
}
#endregion
}
public class data:System.Collections.IList,System.Collections.IEnumerator
{
protected System.Collections.ArrayList _dataitems;
protected int _ptr=0;
public data()
{
_dataitems=new System.Collections.ArrayList();
}
IList 成员#region IList 成员
public bool IsReadOnly
{
get
{
// TODO: 添加 data.IsReadOnly getter 实现
return false;
}
}
public object this[int index]
{
get
{
return _dataitems[index];
}
set
{
_dataitems[index]=value;
}
}
public void RemoveAt(int index)
{
if(index>=0 && index<_dataitems.Count)
_dataitems.RemoveAt(index);
}
public void Insert(int index, object value)
{
if(index>=0 && index<_dataitems.Count)
{
_dataitems.Insert(index,value);
}
}
public void Remove(object value)
{
_dataitems.Remove(value);
}
public bool Contains(object value)
{
return _dataitems.Contains(value);
}
public void Clear()
{
_dataitems.Clear();
}
public int IndexOf(object value)
{
return _dataitems.IndexOf(value);
}
public int Add(object value)
{
return _dataitems.Add(value);
}
public bool IsFixedSize
{
get
{
return _dataitems.IsFixedSize;
}
}
#endregion
ICollection 成员#region ICollection 成员
public bool IsSynchronized
{
get
{
return false;
}
}
public int Count
{
get
{
return _dataitems.Count;
}
}
public void CopyTo(Array array, int index)
{
}
public object SyncRoot
{
get
{
return null;
}
}
#endregion
IEnumerable 成员#region IEnumerable 成员
public System.Collections.IEnumerator GetEnumerator()
{
return this;
}
#endregion
IEnumerator 成员#region IEnumerator 成员
public void Reset()
{
_ptr=0;
}
public object Current
{
get
{
return this[_ptr++];
}
}
public bool MoveNext()
{
if(_ptr<this.Count)
{
return true;
}
else
{
this.Reset();
return false;
}
}
#endregion
}
public class dataitem
{
private string name;
public string ValueName
{
get{return name;}
}
private int valu;
public int Value
{
get{return valu;}
}
public dataitem(int val,string nam)
{
name = nam;
valu = val;
}
}
}
namespace personaltest
{
/**//// <summary>
/// source 的摘要说明。
/// </summary>
public class source:System.ComponentModel.IListSource
{
private data d=new data();
public source()
{
for(int i=0;i<10;i++)
{
d.Add(new dataitem(i,string.Format("this is {0}",i)));
}
}
IListSource 成员#region IListSource 成员
public System.Collections.IList GetList()
{
// TODO: 添加 source.GetList 实现
return d;
}
public bool ContainsListCollection
{
get
{
// TODO: 添加 source.ContainsListCollection getter 实现
return false;
}
}
#endregion
}
public class data:System.Collections.IList,System.Collections.IEnumerator
{
protected System.Collections.ArrayList _dataitems;
protected int _ptr=0;
public data()
{
_dataitems=new System.Collections.ArrayList();
}
IList 成员#region IList 成员
public bool IsReadOnly
{
get
{
// TODO: 添加 data.IsReadOnly getter 实现
return false;
}
}
public object this[int index]
{
get
{
return _dataitems[index];
}
set
{
_dataitems[index]=value;
}
}
public void RemoveAt(int index)
{
if(index>=0 && index<_dataitems.Count)
_dataitems.RemoveAt(index);
}
public void Insert(int index, object value)
{
if(index>=0 && index<_dataitems.Count)
{
_dataitems.Insert(index,value);
}
}
public void Remove(object value)
{
_dataitems.Remove(value);
}
public bool Contains(object value)
{
return _dataitems.Contains(value);
}
public void Clear()
{
_dataitems.Clear();
}
public int IndexOf(object value)
{
return _dataitems.IndexOf(value);
}
public int Add(object value)
{
return _dataitems.Add(value);
}
public bool IsFixedSize
{
get
{
return _dataitems.IsFixedSize;
}
}
#endregion
ICollection 成员#region ICollection 成员
public bool IsSynchronized
{
get
{
return false;
}
}
public int Count
{
get
{
return _dataitems.Count;
}
}
public void CopyTo(Array array, int index)
{
}
public object SyncRoot
{
get
{
return null;
}
}
#endregion
IEnumerable 成员#region IEnumerable 成员
public System.Collections.IEnumerator GetEnumerator()
{
return this;
}
#endregion
IEnumerator 成员#region IEnumerator 成员
public void Reset()
{
_ptr=0;
}
public object Current
{
get
{
return this[_ptr++];
}
}
public bool MoveNext()
{
if(_ptr<this.Count)
{
return true;
}
else
{
this.Reset();
return false;
}
}
#endregion
}
public class dataitem
{
private string name;
public string ValueName
{
get{return name;}
}
private int valu;
public int Value
{
get{return valu;}
}
public dataitem(int val,string nam)
{
name = nam;
valu = val;
}
}
}
需要注意的地方,IEnumerator接口的movenext
的话就不能在movenext()方法中递增指针
引用:http://www.cnblogs.com/zpisgod/articles/70024.html