且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我们如何将List转换为DataTable?

更新时间:2023-02-14 10:25:43

如果您的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 your CMInfo 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;
}