创建自定义数据源

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

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

InBlock.gif               private int valu;
InBlock.gif               public int Value
ExpandedSubBlockStart.gif               {
ExpandedSubBlockStart.gif                       get{return valu;}
ExpandedSubBlockEnd.gif               }

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

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

ExpandedSubBlockEnd.gif               }

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

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

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

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

ExpandedSubBlockEnd.gif               }

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

ExpandedSubBlockEnd.gif               }

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

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

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

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

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

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

ExpandedSubBlockEnd.gif               }

InBlock.gif
ExpandedSubBlockEnd.gif               #endregion

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

ExpandedSubBlockEnd.gif               }

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

ExpandedSubBlockEnd.gif               }

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

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

ExpandedSubBlockEnd.gif               }

InBlock.gif
ExpandedSubBlockEnd.gif               #endregion

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

InBlock.gif
ExpandedSubBlockEnd.gif               #endregion

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

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

ExpandedSubBlockEnd.gif               }

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

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

InBlock.gif               private int valu;
InBlock.gif               public int Value
ExpandedSubBlockStart.gif               {
ExpandedSubBlockStart.gif                       get{return valu;}
ExpandedSubBlockEnd.gif               }
 
InBlock.gif               public dataitem(int val,string nam)
ExpandedSubBlockStart.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()中。
相关文章
|
9天前
|
存储 监控 数据库连接
数据源管理
数据源管理
10 1
|
3月前
|
数据采集 运维 DataWorks
DataWorks产品使用合集之如何更改修改了mc数据源名称的表的绑定引擎实例
DataWorks作为一站式的数据开发与治理平台,提供了从数据采集、清洗、开发、调度、服务化、质量监控到安全管理的全套解决方案,帮助企业构建高效、规范、安全的大数据处理体系。以下是对DataWorks产品使用合集的概述,涵盖数据处理的各个环节。
|
4月前
|
存储 弹性计算 运维
自定义问候语
【4月更文挑战第30天】
22 0
|
4月前
|
缓存 分布式计算 DataWorks
DataWorks常见问题之新增数据源的时候holo类型数据源点击无反应如何解决
DataWorks是阿里云提供的一站式大数据开发与管理平台,支持数据集成、数据开发、数据治理等功能;在本汇总中,我们梳理了DataWorks产品在使用过程中经常遇到的问题及解答,以助用户在数据处理和分析工作中提高效率,降低难度。
|
Unix 关系型数据库 程序员
自定义伟大👑
C语言作为一种经典而强大的编程语言,在计算机科学领域有着广泛的应用。它的简洁性、高效性以及跨平台特性使得C语言成为了开发系统级软件、嵌入式系统以及大规模应用程序的首选。本文将介绍C语言的起源和发展,分析其特点和优势,同时讨论一些常见的应用场景和实例
|
4月前
|
DataWorks 关系型数据库 MySQL
DataWorks数据源暂未绑定数据源,绑定时却显示已经有 1 个是什么回事?
DataWorks数据源暂未绑定数据源,绑定时却显示已经有 1 个是什么回事?
48 2
|
大数据 开发者
R 的数据源导入方法| 学习笔记
快速学习 R 的数据源导入方法
119 0
|
数据库
【视频】配置信息管理 的 使用方法(二):建表、添加元数据
  视频内容是如何用配置信息管理程序查看数据库文档、建表、添加元数据。   感谢 svnhost.cn 提供空间。    
592 0
|
存储 流计算 SQL