IList To DataTable

时间:2014-09-18 16:10:14   收藏:0   阅读:266
public DataTable IListOut(IList<ViewUserInfo> ResList)
    {
        DataTable TempDT = new DataTable();

        //此处遍历IList的结构并建立同样的DataTable
        System.Reflection.PropertyInfo[] p = ResList[0].GetType().GetProperties();
        foreach (System.Reflection.PropertyInfo pi in p)
        {
            TempDT.Columns.Add(pi.Name, System.Type.GetType(pi.PropertyType.ToString()));
        }

        for (int i = 0; i < ResList.Count; i++)
        {
            ArrayList TempList = new ArrayList();
            //将IList中的一条记录写入ArrayList
            foreach (System.Reflection.PropertyInfo pi in p)
            {
                object oo = pi.GetValue(ResList[i], null);
                TempList.Add(oo);
            }

            object[] itm = new object[p.Length];
            //遍历ArrayList向object[]里放数据
            for (int j = 0; j < TempList.Count; j++)
            {
                itm.SetValue(TempList[j], j);
            }
            //将object[]的内容放入DataTable
            TempDT.LoadDataRow(itm, true);
        }
        //返回DataTable
        return TempDT;
    }

 

原文:http://www.cnblogs.com/hausthy/p/3979193.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!