看到一则使用CollectionBase为父类创建自定义数据源的例子:
 using System;
using System;
 namespace 自定义数据源
namespace 自定义数据源
 {
{
 /// <summary>
       /// <summary>
 /// 自定义数据源
       /// 自定义数据源
 /// </summary>
       /// </summary>
 public class cusdatasource : System.Collections.CollectionBase
       public class cusdatasource : System.Collections.CollectionBase
 {
       {

 public cusdatasource()
                       public cusdatasource()
 {
                       {
 for(int i = 0;i < 10;i++)
                               for(int i = 0;i < 10;i++)
 {
                               {
 base.InnerList.Add(new Element(i,string.Format("a[{0}]",i)));
                                       base.InnerList.Add(new Element(i,string.Format("a[{0}]",i)));
 }
                               }
 }
                       }
 }
       }

 public class Element
       public class Element
 {
       {
 private string name;
               private string name;
 public string ValueName
               public string ValueName
 {
               {
 get{return name;}
                       get{return name;}
 }
               }
 private int valu;
               private int valu;
 public int Value
               public int Value
 {
               {
 get{return valu;}
                       get{return valu;}
 }
               }
 public Element(int val,string nam)
               public Element(int val,string nam)
 {
               {
 name = nam;
                       name = nam;
 valu = val;
                       valu = val;
 }
               }
 }
       }
 }
} 
然后我们new一个cusdatasource,并绑定到datagrid上就会出现2列:value和name;
我们还可以通过实现IListSource 或 IEnumerable 接口,来制作自定义的数据源,较上面的麻烦一点,不过更灵活:
 using System;
using System;

 namespace personaltest
namespace personaltest
 {
{
 /// <summary>
       /// <summary>
 /// source 的摘要说明。
       /// source 的摘要说明。
 /// </summary>
       /// </summary>
 public class source:System.ComponentModel.IListSource
       public class source:System.ComponentModel.IListSource
 {
       {
 private data d=new data();
               private data d=new data();
 public source()
               public source()
 {
               {
 for(int i=0;i<10;i++)
                       for(int i=0;i<10;i++)
 {
                       { 
 d.Add(new dataitem(i,string.Format("this is {0}",i)));
                               d.Add(new dataitem(i,string.Format("this is {0}",i)));
 }
                       }
 }
               }
 #region IListSource 成员
               #region IListSource 成员

 public System.Collections.IList GetList()
               public System.Collections.IList GetList()
 {
               {
 // TODO:  添加 source.GetList 实现
                       // TODO:  添加 source.GetList 实现
 return d;
                       return d;
 }
               }

 public bool ContainsListCollection
               public bool ContainsListCollection
 {
               {
 get
                       get
 {
                       {
 // TODO:  添加 source.ContainsListCollection getter 实现
                               // TODO:  添加 source.ContainsListCollection getter 实现
 return false;
                               return false;
 }
                       }
 }
               }

 #endregion
               #endregion
 }
       }

 public class data:System.Collections.IList,System.Collections.IEnumerator
       public class data:System.Collections.IList,System.Collections.IEnumerator
 {
       {
 protected System.Collections.ArrayList _dataitems;
               protected System.Collections.ArrayList _dataitems;
 protected int _ptr=0;
               protected int _ptr=0;
 public data()
               public data()
 {
               {
 _dataitems=new System.Collections.ArrayList();
                       _dataitems=new System.Collections.ArrayList();
 }
               }
 #region IList 成员
               #region IList 成员

 public bool IsReadOnly
               public bool IsReadOnly
 {
               {
 get
                       get
 {
                       {
 // TODO:  添加 data.IsReadOnly getter 实现
                               // TODO:  添加 data.IsReadOnly getter 实现
 return false;
                               return false;
 }
                       }
 }
               }

 public object this[int index]
               public object this[int index]
 {
               {
 get
                       get
 {
                       {

 return _dataitems[index];
                               return _dataitems[index];
 }
                       }
 set
                       set
 {
                       {
 _dataitems[index]=value;
                               _dataitems[index]=value;
 }
                       }
 }
               }

 public void RemoveAt(int index)
               public void RemoveAt(int index)
 {
               {
 if(index>=0 && index<_dataitems.Count)
                       if(index>=0 && index<_dataitems.Count)
 _dataitems.RemoveAt(index);
                               _dataitems.RemoveAt(index);
 }
               }

 public void Insert(int index, object value)
               public void Insert(int index, object value)
 {
               {
 if(index>=0 && index<_dataitems.Count)
                       if(index>=0 && index<_dataitems.Count)
 {
                       {
 _dataitems.Insert(index,value);
                               _dataitems.Insert(index,value);
 }
                       }
 }
               }

 public void Remove(object value)
               public void Remove(object value)
 {
               {
 _dataitems.Remove(value);
                       _dataitems.Remove(value);
 }
               }

 public bool Contains(object value)
               public bool Contains(object value)
 {
               {
 return _dataitems.Contains(value);
                       return _dataitems.Contains(value);
 }
               }

 public void Clear()
               public void Clear()
 {
               {
 _dataitems.Clear();
                       _dataitems.Clear();
 }
               }

 public int IndexOf(object value)
               public int IndexOf(object value)
 {
               {
 return _dataitems.IndexOf(value);
                       return _dataitems.IndexOf(value);
 }
               }

 public int Add(object value)
               public int Add(object value)
 {
               {
 return _dataitems.Add(value);
                       return _dataitems.Add(value);
 }
               }

 public bool IsFixedSize
               public bool IsFixedSize
 {
               {
 get
                       get
 {
                       {
 return _dataitems.IsFixedSize;
                               return _dataitems.IsFixedSize;
 }
                       }
 }
               }

 #endregion
               #endregion

 #region ICollection 成员
               #region ICollection 成员

 public bool IsSynchronized
               public bool IsSynchronized
 {
               {
 get
                       get
 {
                       {
 return false;
                               return false;
 }
                       }
 }
               }

 public int Count
               public int Count
 {
               {
 get
                       get
 {
                       {
 return _dataitems.Count;
                             return _dataitems.Count;
 }
                       }
 }
               }

 public void CopyTo(Array array, int index)
               public void CopyTo(Array array, int index)
 {
               {
 }
               }

 public object SyncRoot
               public object SyncRoot
 {
               {
 get
                       get
 {
                       {
 return null;
                               return null;
 }
                       }
 }
               }

 #endregion
               #endregion

 #region IEnumerable 成员
               #region IEnumerable 成员

 public System.Collections.IEnumerator GetEnumerator()
               public System.Collections.IEnumerator GetEnumerator()
 {
               {
 return this;
                       return this;
 }
               }

 #endregion
               #endregion

 #region IEnumerator 成员
               #region IEnumerator 成员

 public void Reset()
               public void Reset()
 {
               {
 _ptr=0;
                       _ptr=0;
 }
               }

 public object Current
               public object Current
 {
               {
 get
                       get
 {
                       {
 return this[_ptr++];
                               return this[_ptr++];
 }
                       }
 }
               }

 public bool MoveNext()
               public bool MoveNext()
 {
               {
 if(_ptr<this.Count)
                       if(_ptr<this.Count)
 {
                       {
 return true;
                               return true;
 }
                       }
 else
                       else
 {
                       {
 this.Reset();
                               this.Reset();
 return false;
                               return false;
 }
                       }
 }
               }

 #endregion
               #endregion
 }
       }

 public class dataitem
       public class dataitem
 {
       {
 private string name;
               private string name;
 public string ValueName
               public string ValueName
 {
               {
 get{return name;}
                       get{return name;}
 }
               }
 private int valu;
               private int valu;
 public int Value
               public int Value
 {
               {
 get{return valu;}
                       get{return valu;}
 }
               } 
 public dataitem(int val,string nam)
               public dataitem(int val,string nam)
 {
               {
 name = nam;
                       name = nam;
 valu = val;
                       valu = val;
 }
               }
 }
       }
 }
}
 实现了IListSource接口的自定义数据源
 实现了IListSource接口的自定义数据源,IEnumerable在其中也有实现;
需要注意的地方,IEnumerator接口的movenext()方法,在foreach语句的时候会先执行一次,然后才会用current()方法返回"当前值",所以指针初始化为0
的话就不能在movenext()方法中递增指针,而应该放在current()中。
引用:http://www.cnblogs.com/zpisgod/articles/70024.html      
 using System;
using System; namespace 自定义数据源
namespace 自定义数据源 {
{ /// <summary>
       /// <summary> /// 自定义数据源
       /// 自定义数据源 /// </summary>
       /// </summary> public class cusdatasource : System.Collections.CollectionBase
       public class cusdatasource : System.Collections.CollectionBase {
       {
 public cusdatasource()
                       public cusdatasource() {
                       { for(int i = 0;i < 10;i++)
                               for(int i = 0;i < 10;i++) {
                               { base.InnerList.Add(new Element(i,string.Format("a[{0}]",i)));
                                       base.InnerList.Add(new Element(i,string.Format("a[{0}]",i))); }
                               } }
                       } }
       }
 public class Element
       public class Element {
       { private string name;
               private string name; public string ValueName
               public string ValueName {
               { get{return name;}
                       get{return name;} }
               } private int valu;
               private int valu; public int Value
               public int Value {
               { get{return valu;}
                       get{return valu;} }
               } public Element(int val,string nam)
               public Element(int val,string nam) {
               { name = nam;
                       name = nam; valu = val;
                       valu = val; }
               } }
       } }
} 然后我们new一个cusdatasource,并绑定到datagrid上就会出现2列:value和name;
我们还可以通过实现IListSource 或 IEnumerable 接口,来制作自定义的数据源,较上面的麻烦一点,不过更灵活:
 using System;
using System;
 namespace personaltest
namespace personaltest {
{ /// <summary>
       /// <summary> /// source 的摘要说明。
       /// source 的摘要说明。 /// </summary>
       /// </summary> public class source:System.ComponentModel.IListSource
       public class source:System.ComponentModel.IListSource {
       { private data d=new data();
               private data d=new data(); public source()
               public source() {
               { for(int i=0;i<10;i++)
                       for(int i=0;i<10;i++) {
                       {  d.Add(new dataitem(i,string.Format("this is {0}",i)));
                               d.Add(new dataitem(i,string.Format("this is {0}",i))); }
                       } }
               } #region IListSource 成员
               #region IListSource 成员
 public System.Collections.IList GetList()
               public System.Collections.IList GetList() {
               { // TODO:  添加 source.GetList 实现
                       // TODO:  添加 source.GetList 实现 return d;
                       return d; }
               }
 public bool ContainsListCollection
               public bool ContainsListCollection {
               { get
                       get {
                       { // TODO:  添加 source.ContainsListCollection getter 实现
                               // TODO:  添加 source.ContainsListCollection getter 实现 return false;
                               return false; }
                       } }
               }
 #endregion
               #endregion }
       }
 public class data:System.Collections.IList,System.Collections.IEnumerator
       public class data:System.Collections.IList,System.Collections.IEnumerator {
       { protected System.Collections.ArrayList _dataitems;
               protected System.Collections.ArrayList _dataitems; protected int _ptr=0;
               protected int _ptr=0; public data()
               public data() {
               { _dataitems=new System.Collections.ArrayList();
                       _dataitems=new System.Collections.ArrayList(); }
               } #region IList 成员
               #region IList 成员
 public bool IsReadOnly
               public bool IsReadOnly {
               { get
                       get {
                       { // TODO:  添加 data.IsReadOnly getter 实现
                               // TODO:  添加 data.IsReadOnly getter 实现 return false;
                               return false; }
                       } }
               }
 public object this[int index]
               public object this[int index] {
               { get
                       get {
                       {
 return _dataitems[index];
                               return _dataitems[index]; }
                       } set
                       set {
                       { _dataitems[index]=value;
                               _dataitems[index]=value; }
                       } }
               }
 public void RemoveAt(int index)
               public void RemoveAt(int index) {
               { if(index>=0 && index<_dataitems.Count)
                       if(index>=0 && index<_dataitems.Count) _dataitems.RemoveAt(index);
                               _dataitems.RemoveAt(index); }
               }
 public void Insert(int index, object value)
               public void Insert(int index, object value) {
               { if(index>=0 && index<_dataitems.Count)
                       if(index>=0 && index<_dataitems.Count) {
                       { _dataitems.Insert(index,value);
                               _dataitems.Insert(index,value); }
                       } }
               }
 public void Remove(object value)
               public void Remove(object value) {
               { _dataitems.Remove(value);
                       _dataitems.Remove(value); }
               }
 public bool Contains(object value)
               public bool Contains(object value) {
               { return _dataitems.Contains(value);
                       return _dataitems.Contains(value); }
               }
 public void Clear()
               public void Clear() {
               { _dataitems.Clear();
                       _dataitems.Clear(); }
               }
 public int IndexOf(object value)
               public int IndexOf(object value) {
               { return _dataitems.IndexOf(value);
                       return _dataitems.IndexOf(value); }
               }
 public int Add(object value)
               public int Add(object value) {
               { return _dataitems.Add(value);
                       return _dataitems.Add(value); }
               }
 public bool IsFixedSize
               public bool IsFixedSize {
               { get
                       get {
                       { return _dataitems.IsFixedSize;
                               return _dataitems.IsFixedSize; }
                       } }
               }
 #endregion
               #endregion
 #region ICollection 成员
               #region ICollection 成员
 public bool IsSynchronized
               public bool IsSynchronized {
               { get
                       get {
                       { return false;
                               return false; }
                       } }
               }
 public int Count
               public int Count {
               { get
                       get {
                       { return _dataitems.Count;
                             return _dataitems.Count; }
                       } }
               }
 public void CopyTo(Array array, int index)
               public void CopyTo(Array array, int index) {
               { }
               }
 public object SyncRoot
               public object SyncRoot {
               { get
                       get {
                       { return null;
                               return null; }
                       } }
               }
 #endregion
               #endregion
 #region IEnumerable 成员
               #region IEnumerable 成员
 public System.Collections.IEnumerator GetEnumerator()
               public System.Collections.IEnumerator GetEnumerator() {
               { return this;
                       return this; }
               }
 #endregion
               #endregion
 #region IEnumerator 成员
               #region IEnumerator 成员
 public void Reset()
               public void Reset() {
               { _ptr=0;
                       _ptr=0; }
               }
 public object Current
               public object Current {
               { get
                       get {
                       { return this[_ptr++];
                               return this[_ptr++]; }
                       } }
               }
 public bool MoveNext()
               public bool MoveNext() {
               { if(_ptr<this.Count)
                       if(_ptr<this.Count) {
                       { return true;
                               return true; }
                       } else
                       else {
                       { this.Reset();
                               this.Reset(); return false;
                               return false; }
                       } }
               }
 #endregion
               #endregion }
       }
 public class dataitem
       public class dataitem {
       { private string name;
               private string name; public string ValueName
               public string ValueName {
               { get{return name;}
                       get{return name;} }
               } private int valu;
               private int valu; public int Value
               public int Value {
               { get{return valu;}
                       get{return valu;} }
               }  public dataitem(int val,string nam)
               public dataitem(int val,string nam) {
               { name = nam;
                       name = nam; valu = val;
                       valu = val; }
               } }
       } }
} 
 需要注意的地方,IEnumerator接口的movenext
的话就不能在movenext()方法中递增指针
引用:http://www.cnblogs.com/zpisgod/articles/70024.html
| 版权声明:本文原创发表于 博客园,作者为 路过秋天 本文欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则视为侵权。 | 
| 个人微信公众号   | Donation(扫码支持作者):支付宝:   | Donation(扫码支持作者):微信:   |   | 
 


 
        
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号