视图模型列表中的数据表 [英] data table from view models list
本文介绍了视图模型列表中的数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将视图模型列表转换为数据表以便以后以pdf格式打印?
i此行中有错误:values [i] = Props [ i] .GetValue(item,null).ToString();
无法转换它!
how can i convert list of view models to data table to print it in pdf later ?
i have error in this line : values[i] = Props[i].GetValue(item, null).ToString();
cant convert it !
public ActionResult detailsoperation(OperationViewModel operationvm
,
CategorieViewModel categories, string idd)
{
List<CategorieViewModel> list = (List<CategorieViewModel>)Session["categories"];
list.Add(categories);
DataTable dataTable = new DataTable(typeof(CategorieViewModel).Name);
//Get all the properties
PropertyInfo[] Props = typeof(OperationViewModel).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo prop in Props)
{
//Setting column names as Property names
dataTable.Columns.Add(prop.Name);
}
foreach (CategorieViewModel item in list)
{
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).ToString();
}
dataTable.Rows.Add(values);
}
推荐答案
允许我插入拥有 [ ^ ] solution
Allow me to plug my own[^] solution
Solution1 by JörgenAndersson [ ^ ]非常好。除此之外,我建议阅读:如何:实现CopyToDataTable< t>通用类型T不是DataRow的地方 [ ^ ]
Solution1 by Jörgen Andersson[^] is very good. In addition to it, i'd suggest to read this: How to: Implement CopyToDataTable<t> Where the Generic Type T Is Not a DataRow[^]
这篇关于视图模型列表中的数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文