首页 新闻 赞助 找找看

遍历集合输出

0
悬赏园豆:30 [已关闭问题] 关闭于 2009-05-11 16:25

 private IEnumerable getResolvedDataSource(object source)
        {
            if (source is IEnumerable)
                return (IEnumerable)source;
            else if (source is IList)
                return (IEnumerable)source;
            else if (source is DataSet )
                return (IEnumerable)(((DataSet)source).Tables[0].DefaultView);
            else if (source is DataTable)
                return (IEnumerable)(((DataTable)source).DefaultView);
            else
                return null;
        }
        ////用来存放数据的ArrayList,这个也配合ViewState使用
        private ArrayList cacheList = new ArrayList();
        /// <summary>
        /// 处理DataBinding事件,把数据源中需要的数据加到一个ArrayList中。
        /// </summary>
        /// <param name="e">EventArgs</param>
        protected override void OnDataBinding(EventArgs e)
        {
            base.OnDataBinding(e);
            try
            {
                if (this.dataSource != null)
                {
                    IEnumerable source = this.getResolvedDataSource(this.dataSource);
                    IEnumerator item = source.GetEnumerator();
                    if (this.cacheList.Count == 0)
                    {
                        //循环遍历集合
                        while (item.MoveNext())
                        {
                            object ite = item.Current;
                                cacheList.Add(((DataRowView )ite).Row[0] );
                        }
                    }
                }
            }
            catch
            {
                throw new Exception("请输入三个字段的名称");
            }
        }

我通过一个集合存储了数据源的一行数据,这一行数据只有三个字段,我怎么才能将这三个字段的值依次读出来并存到cacheList中?代码应该怎么写?请帮我看下吧!谢谢啦!

宪华的主页 宪华 | 初学一级 | 园豆:0
提问于:2009-05-11 10:16
< >
分享
所有回答(2)
0

关注

成龙 | 园豆:5 (初学一级) | 2009-05-11 10:41
0

创建一个实体类

wentmc | 园豆:35 (初学一级) | 2009-05-11 12:38
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册