ist中以对象为单位,而datatable是以行(row)为单位,这个过程无非就是把一个对象转化为datatable中的行(row)
list转换为datatable代码如下:
//先定义一个list集合
List<mansIntesting> mansIntestings = new List<mansIntesting>();
mansIntestings.Add(new mansIntesting(1, "li", "sldfjldsjl1"));
mansIntestings.Add(new mansIntesting(1, "li", "sldfjldsjl1"));
mansIntestings.Add(new mansIntesting(1, "li", "sldfjldsjl1"));
mansIntestings.Add(new mansIntesting(1, "li", "sldfjldsjl1"));
mansIntestings.Add(new mansIntesting(13, "lis", "sldfjldsjl3"));
mansIntestings.Add(new mansIntesting(1, "li", "sldfjldsjl11"));
//定义一个dataTable
DataTable dt = new DataTable();
dt.Columns.Add("ManId",typeof(int));
dt.Columns.Add("Manname",typeof(string));
dt.Columns.Add("Intesting",typeof(string));
//然后list转换为datatable 实现转换
foreach (var row in mansIntestings)
{
dt.Rows.Add(row.ManId, row.Manname, row.Intesting);
}