首页 > 其他 > 详细

C# 把List 集合转换成DataTable

时间:2014-02-13 02:33:11      阅读:398      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
  /// <summary>
        /// 将List集合 转换成 DataTable
        /// </summary>
        /// <param name="entitys">sellerSearchDealList是我自己定义的一个类</param>
        public static DataTable ListToTable(List<object> entitys)
        {
            DataTable dtresult = new DataTable();
            if (entitys == null || entitys.Count < 1)
            {
                throw new Exception("");
            }
            else
            {
                PropertyInfo[] propertys = entitys[0].GetType().GetProperties();
                foreach (PropertyInfo pi in propertys)
                {
                    dtresult.Columns.Add(pi.Name, pi.PropertyType);
                }

                for (int i = 0; i < entitys.Count; i++)
                {
                    ArrayList tenpList = new ArrayList();
                    foreach (PropertyInfo pi in propertys)
                    {
                        object obj = pi.GetValue(entitys[i], null);
                        tenpList.Add(obj);
                    }
                    object[] array = tenpList.ToArray();
                    dtresult.LoadDataRow(array, true);
                }
            }
            return dtresult;
        }
bubuko.com,布布扣

C# 把List 集合转换成DataTable

原文:http://www.cnblogs.com/jcdd-4041/p/3546343.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!