通过这段代码我得到错误“输入数组比这个表中的列数长”
DataTable dtMain = new DataTable();
var qry = from c in db.Categories where c.SubCategoryName.Contains(txtSearch.Text.Trim()) select c;
foreach (var k in qry)
{
var qry1 = from c in db.ProductMasters where c.ProductNameID == k.CategoryID
select new { ProductID = c.ProductID,
TagID = c.TagID,
ProductNameID = (from d in db.Categories
where d.CategoryID == c.ProductNameID
select d.SubCategoryName).FirstOrDefault(),
CategoryID = (from d in db.Categories
where d.CategoryID == c.CategoryID
select d.CategoryName).FirstOrDefault(),
ProductType = c.ProductType,
Maker = c.Maker,
Model = c.Model,
SerialNo = c.SerialNo,
MACAddress = c.MACAddress,
Warranty = c.Warranty,
Price = c.Price,
ProductOrderNo = c.ProductOrderNo,
VendorID = (from d in db.Vendors
where d.VendorID == c.VendorID
select d.VendorName).FirstOrDefault(),
PurchaseDate = c.PurchaseDate.ToString().Substring(0, 10),
ProductStatus = c.ProductStatus,
Remark = c.Remark };
dtMain.Rows.Add(qry1);
}
gridProduct.DataSource = dtMain;
gridProduct.DataBind();
答案 0 :(得分:0)
数据表适用于行和列概念。你需要先提到所有的列名。像这样在数据表中添加行。
DataTable dtMain = new DataTable();
for (int i = 0; i < rows .Count(); i++)
{
DataRow datarowObj= dtMain .NewRow();
datarowObj["A"] = yourValue;
datarowObj["B"] = yourValue;
datarowObj["C"] = yourValue;
dtMain .Rows.Add(datarowObj);
}
或者像SLAKS提到的关于bindingList。您可以使用。的BindingList
return new BindingList<YourClass>(qry1.ToList())