这可能甚至不可能,但我有一个例如数据的网格:
我想填充第二个gridview,显示该项目的项目和订单,如下所示:
这甚至可能吗?考虑做这样的FOREACH循环,但不知道我将如何按项#分组,然后为该项的每个订单做一个i ++?
DataTable dtProducts = new DataTable();
// Items for the product Gridview
foreach (GridViewRow row in ordersGridView.Rows)
{
var products = from orderRow in dt.AsEnumerable()
group orderRow["orderNo"] by orderRow["itemNo"] into grp
select new { product = grp.Key, orders = grp.Count() };
dt.Rows.Clear();
foreach (var prod in products)
{
DataRow r = dtProducts.NewRow();
r["PRODUCTS"] = prod.product;
r["ORDERS"] = prod.orders;
dtProducts.Rows.Add(r);
}
}
productGridView.DataSource = dtProducts;
productGridView.DataBind();
答案 0 :(得分:0)
假设您的第一个表至少有两个字段:ORDERID和ITEM:
首先,查询将为您提供包含两个字段的可枚举项目 - 产品和订单计数。那么,如果你想要它作为DataTable,你可以循环查询结果。
var products = from orderRow in dtOrders.AsEnumerable()
group orderRow["ORDERID"] by orderRow["ITEM"] into grp
select new { product = grp.Key, orders = grp.Count() };
dtProducts.Rows.Clear();
foreach (var prod in products)
{
DataRow r = dtProducts.NewRow();
r["PRODUCTS"] = prod.product;
r["ORDERS"] = prod.orders;
dtProducts.Rows.Add(r);
}
我认为可以将这两个步骤结合起来,但这应该至少让你开始:)
答案 1 :(得分:0)
让这个工作并且效果很好......谢谢大家:
var result = from row in dt.AsEnumerable()
group row["orderNo"] by row["itemNo"] into grp
select new
{
itemNum = grp.Key,
orders = grp.Count()
};
foreach (var t in result)
{
dtProducts.Rows.Add(t.itemNum, t.orders, "", "");
}