从列表中填充DataTable

时间:2014-03-18 12:13:35

标签: c# asp.net-mvc list datatable

我有一个列表和数据表,如下所示

  //list to contain the dummy products
        List<Product> lst = new List<Product>();
        public DataTable table = new DataTable();

        public ActionResult Index()
        { 
            var model = new ProductRangeViewModel();

            //dummy products
            lst.Add(new Product() { Status = "Live", SupplierProductCode = 12345, MarketPlaceProductCode = 774589, EANbarcode = 74521569853257, Description = "Toaster", CaseSize = "1", PackSize = "Single", DateLoaded = Convert.ToDateTime(23 / 02 / 2013), ReqActivationDate = Convert.ToDateTime(23 / 03 / 2014), PriceExVAT = 29.99, VATpercentage = "20" });
            lst.Add(new Product() { Status = "In Review", SupplierProductCode = 45525, MarketPlaceProductCode = 771125, EANbarcode = 88854125632589, Description = "Scissors", CaseSize = "1", PackSize = "Single", DateLoaded = Convert.ToDateTime(12 / 03 / 2013), ReqActivationDate = Convert.ToDateTime(23 / 03 / 2014), PriceExVAT = 42.99, VATpercentage = "20" });
            lst.Add(new Product() { Status = "Deleted", SupplierProductCode = 86423, MarketPlaceProductCode = 552369, EANbarcode = 74586259871202, Description = "mobile", CaseSize = "1", PackSize = "Single", DateLoaded = Convert.ToDateTime(15 / 04 / 2013), ReqActivationDate = Convert.ToDateTime(23 / 03 / 2014), PriceExVAT = 23.56, VATpercentage = "20" });
            lst.Add(new Product() { Status = "Suspended", SupplierProductCode = 74522, MarketPlaceProductCode = 741256, EANbarcode = 75210256987425, Description = "Gun", CaseSize = "1", PackSize = "Single", DateLoaded = Convert.ToDateTime(22 / 06 / 2013), ReqActivationDate = Convert.ToDateTime(23 / 03 / 2014), PriceExVAT = 85.36, VATpercentage = "20" });
            lst.Add(new Product() { Status = "Live", SupplierProductCode = 41258, MarketPlaceProductCode = 744415, EANbarcode = 96325698741205, Description = "Television", CaseSize = "1", PackSize = "Single", DateLoaded = Convert.ToDateTime(08 / 08 / 2013), ReqActivationDate = Convert.ToDateTime(23 / 03 / 2014), PriceExVAT = 77.58, VATpercentage = "20" });
            lst.Add(new Product() { Status = "Rejected", SupplierProductCode = 72146, MarketPlaceProductCode = 745889, EANbarcode = 10002365802541, Description = "Cycle", CaseSize = "1", PackSize = "Single", DateLoaded = Convert.ToDateTime(03 / 12 / 2013), ReqActivationDate = Convert.ToDateTime(23 / 03 / 2014), PriceExVAT = 140.00, VATpercentage = "20" });
            model.product = lst;

            foreach(Product prod in lst)
            {

            }

我正在尝试用列表填写dataTable。

但我无法在我的智慧中获得DataColumn.Add。

我该如何处理?

注意:它的MVC的控制器页面代码。

1 个答案:

答案 0 :(得分:0)

我不知道DataColumns.Add方法。你试过吗

table.Columns.Add(prod, typeof(Product));

或者您是在寻找为对象的每个属性添加列的方法,并为每个实例添加行?