我有一些财产的数据列表。我想将该列表数据转换为数据表。如何将列表转换为datable。如何列表转换成数据表
回答
只需添加此功能,并调用它,它就会列表转换为数据表。
public static DataTable ToDataTable<T>(List<T> items)
{
DataTable dataTable = new DataTable(typeof(T).Name);
//Get all the properties
PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo prop in Props)
{
//Defining type of data column gives proper data table
var type = (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>) ? Nullable.GetUnderlyingType(prop.PropertyType) : prop.PropertyType);
//Setting column names as Property names
dataTable.Columns.Add(prop.Name, type);
}
foreach (T item in items)
{
var values = new object[Props.Length];
for (int i = 0; i < Props.Length; i++)
{
//inserting property values to datatable rows
values[i] = Props[i].GetValue(item, null);
}
dataTable.Rows.Add(values);
}
//put a breakpoint here and check datatable
return dataTable;
}
如何调用该function.thanks答复 – user2660267
假设你有User.List
我们是否需要为此方法添加任何名称空间才能工作? PropertyInfo []没有被识别。 –
你可以使用这个扩展方法,并调用它。
DataTable dt = YourList.ToDataTable();
public static DataTable ToDataTable<T>(this List<T> iList)
{
DataTable dataTable = new DataTable();
PropertyDescriptorCollection propertyDescriptorCollection =
TypeDescriptor.GetProperties(typeof(T));
for (int i = 0; i < propertyDescriptorCollection.Count; i++)
{
PropertyDescriptor propertyDescriptor = propertyDescriptorCollection[i];
Type type = propertyDescriptor.PropertyType;
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
type = Nullable.GetUnderlyingType(type);
dataTable.Columns.Add(propertyDescriptor.Name, type);
}
object[] values = new object[propertyDescriptorCollection.Count];
foreach (T iListItem in iList)
{
for (int i = 0; i < values.Length; i++)
{
values[i] = propertyDescriptorCollection[i].GetValue(iListItem);
}
dataTable.Rows.Add(values);
}
return dataTable;
}
正是我在找的东西! –
这给了我'string'类型'List'的数值。 – Jogi
static DataTable ConvertListToDataTable(List<string[]> list)
{
// New table.
DataTable table = new DataTable();
// Get max columns.
int columns = 0;
foreach (var array in list)
{
if (array.Length > columns)
{
columns = array.Length;
}
}
// Add columns.
for (int i = 0; i < columns; i++)
{
table.Columns.Add();
}
// Add rows.
foreach (var array in list)
{
table.Rows.Add(array);
}
return table;
}
protected void Button1_Click(object sender, EventArgs e)
{
List<string[]> list = new List<string[]>();
list.Add(new string[] { "Column 1", "Column 2", "Column 3" });
list.Add(new string[] { "Row 2", "Row 2" });
list.Add(new string[] { "Row 3" });
// Convert to DataTable.
DataTable table = ConvertListToDataTable(list);
dataGridView1.DataSource = table;
}
试试这个
'ConvertListToDataTable(...);'在dotnet的,除非你访问这个页面,你可以为这个方法找到的代码是不知道'http://social.msdn.microsoft.com/Forums/vstudio/en-US/6ffcb247 -77fb-40B4-BCBA-08ba377ab9db /转换-A-列表 - – Bellash
- 1. 如何列表转换为数据表
- 2. 整数列表如何转换成列表的列表?
- 3. 如何将数据框列表转换为列表列表?
- 4. 如何转MySQL表行转换成列
- 5. 如何转换数据表?
- 6. 转换R数据表列
- 7. 转换列表数据帧
- 8. 如何JSON数据与表转换成使用动态列
- 9. 如何将列表数据转换成JSON格式
- 10. 如何将数据转换成列表蟒蛇
- 11. 转换一个列表转换成数据帧
- 12. 如何列表转换成字典
- 13. 如何层次列表转换成JSON
- 14. 如何将XML转换成列表
- 15. 如何将2维数组列表转换为数据表?
- 16. 转换的数据表列表,以JSON
- 17. 将数据表转换为列表
- 18. 列表索引转换为数据表
- 19. 转换列表到数据表
- 20. 转换数据表到泛型列表
- 21. 如何显示从Excel工作表数据转换成图表
- 22. 组列表转换成由列表
- 23. 如何将Python整数列表转换为列表列表?
- 24. 如何使用隐式转换列表转换[A]成列表[B]
- 25. 转换规则转换成列表
- 26. XSLT转换HTML列表转换成XML
- 27. 转换成向量列表
- 28. 转换成列表解析
- 29. 如何元组(含4元)的列表转换成列表
- 30. 如何列表转换为Unicode列表
你能不能给我们介绍一下类型列表中的对象的信息? –