回覆列表
  • 1 # lanfengz3

    程式碼如下:

    ///

    /// 將List轉換成DataTable

    ///

    ///

    ///

    ///

    public static DataTable ToDataTable

    {

    PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));

    DataTable dt = new DataTable();

    for (int i = 0; i

    {

    PropertyDescriptor property = properties[i];

    dt.Columns.Add(property.Name, property.PropertyType);

    }

    object[] values = new object[properties.Count];

    foreach (T item in data)

    {

    for (int i = 0; i

    {

    values[i] = properties[i].GetValue(item);

    }

    dt.Rows.Add(values);

    }

    return dt;

    }

  • 中秋節和大豐收的關聯?
  • 水母怎樣形容?