提问如题 ,list是动态的,如何可以以最优方式将list<model> 动态的加入到datatable中?谢谢 提问如题 ,list是动态的,如何可以以最优方式将list<model> 动态的加入到datatable中?谢谢listdatatable 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public DataTable ConvertToDataTable<T>(IList<T> data) { PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T)); DataTable table = new DataTable(); foreach (PropertyDescriptor prop in properties) table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType); foreach (T item in data) { DataRow row = table.NewRow(); foreach (PropertyDescriptor prop in properties) row[prop.Name] = prop.GetValue(item) ?? DBNull.Value; table.Rows.Add(row); } return table; } Dev控件RadioGroup怎么用的? 求电脑快捷键的技巧? 有关事务的问题 一个简单的问题,高手请帮忙 C#怎么还原这段js,winform下 关于C1CommandMenu的问题! 如何把字符串追加到文本文件中? 菜鸟请教 几个关于 C#中 目录与文件的问题 怎样有选择的打开Excel文件 为什么数据库不能立即刷新??? C# winform 拷贝文件 未响应 如何解决 关于
public DataTable ConvertToDataTable<T>(IList<T> data)
{
PropertyDescriptorCollection properties =
TypeDescriptor.GetProperties(typeof(T));
DataTable table = new DataTable();
foreach (PropertyDescriptor prop in properties)
table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
foreach (T item in data)
{
DataRow row = table.NewRow();
foreach (PropertyDescriptor prop in properties)
row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
table.Rows.Add(row);
}
return table; }