悟生慧

 

ArrayList转换类型为DataTable类型

        /// <summary>
        /// ArrayList转换类型为DataTable
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static DataTable ToDataTableTow(IList list)
        {
            DataTable result = new DataTable();

            if (list.Count > 0)
            {
                PropertyInfo[] propertys = list[0].GetType().GetProperties();

                foreach (PropertyInfo pi in propertys)
                {

                    result.Columns.Add(pi.Name, pi.PropertyType);

                }
                for (int i = 0; i < list.Count; i++)
                {

                    ArrayList tempList = new ArrayList();

                    foreach (PropertyInfo pi in propertys)
                    {

                        object obj = pi.GetValue(list[i], null);

                        tempList.Add(obj);
                    }

                    object[] array = tempList.ToArray();

                    result.LoadDataRow(array, true);
                }
            }
            return result;
        }

posted on 2011-06-21 15:56  悟生慧  阅读(2378)  评论(0编辑  收藏  举报

导航