程式碼如下:
///
/// 將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)
values[i] = properties[i].GetValue(item);
dt.Rows.Add(values);
return dt;
程式碼如下:
///
/// 將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;
}