我们如何将List转换为DataTable? [英] How ca we convert the List into DataTable?
本文介绍了我们如何将List转换为DataTable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好!有人可以帮我将List转换成DataTable吗?我确实喜欢这样
Hi everyone! Can any one help me in converting the List into DataTable?I did like this
DataTable dt1 =(DataTable) DataDecl.CMInfo.ToList();
DataSet ds = new System.Data.DataSet();
ds.Tables.Add(DataDecl.CMInfo.ToList());
这是正确的方法吗?
Is this the right way?
推荐答案
如果您的CMInfo
是自定义列表,则它希望隐式转换为datatable,
您需要为此付出一些努力.
看一下.
使用泛型和属性从集合到数据表的投射 [ http://beecy.net/post/2009/05/18/convert-generic-list-to-datatable-using-reflection [ http://forums.silverlight.net/forums/p/99187/306445 [ ^ ]
if yourCMInfo
a custom list then it want implicitely convert to datatable,
You need some efforts for this.
Take a Look.
Casting from a Collection to a Data Table using Generics and Attributes[^]
http://beecy.net/post/2009/05/18/convert-generic-list-to-datatable-using-reflection[^]
http://forums.silverlight.net/forums/p/99187/306445[^]
Try this..
public static DataTable ListToDataTable(List<T> mylist)
{
DataTable dt = new DataTable();
dt.Columns.Add("myCol", mylist[0].IntField.GetType());
foreach (MyRecord rc in mylist)
{
dt.Rows.Add(rc.myCol);
}
return dt;
}
这篇关于我们如何将List转换为DataTable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文