zoukankan      html  css  js  c++  java
  • list 转换成datatable

    感谢网上的一位朋友

     1     /// <summary>
     2  60         /// 将集合类转换成DataTable
     3  61         /// </summary>
     4  62         /// <param name="list">集合</param>
     5  63         /// <returns></returns>
     6  64         public static DataTable ToDataTable(IList list)
     7  65         {
     8  66             DataTable result = new DataTable();
     9  67             if (list.Count > 0)
    10  68             {
    11  69                 PropertyInfo[] propertys = list[0].GetType().GetProperties();
    12  70                 foreach (PropertyInfo pi in propertys)
    13  71                 {
    14  72                     result.Columns.Add(pi.Name, pi.PropertyType);
    15  73                 }
    16  74 
    17  75                 for (int i = 0; i < list.Count; i++)
    18  76                 {
    19  77                     ArrayList tempList = new ArrayList();
    20  78                     foreach (PropertyInfo pi in propertys)
    21  79                     {
    22  80                         object obj = pi.GetValue(list[i], null);
    23  81                         tempList.Add(obj);
    24  82                     }
    25  83                     object[] array = tempList.ToArray();
    26  84                     result.LoadDataRow(array, true);
    27  85                 }
    28  86             }
    29  87             return result;
    30  88         }
    View Code
  • 相关阅读:
    算法第二章上机实践报告
    算法第一章作业
    第7章学习小结 不使用STL-map过实践题:QQ帐户的申请与登陆
    第6章学习小结
    HDU
    HDU 2089 不要62(数位DP)
    char-2
    chart-7
    chart-6
    char-8
  • 原文地址:https://www.cnblogs.com/yuanjiehot/p/4352337.html
Copyright © 2011-2022 走看看