C# system.linq对gridview中具有相同ID的行求和
同样的问题再次出现,但这次是在gridview中对相同ID的行求和。我的gridview中有两列,分别名为ID和quantity。议程是我必须手动输入ID和数量,并且我试图用相同的行ID汇总数量 ID数量C# system.linq对gridview中具有相同ID的行求和,c#,linq,datagridview,C#,Linq,Datagridview,同样的问题再次出现,但这次是在gridview中对相同ID的行求和。我的gridview中有两列,分别名为ID和quantity。议程是我必须手动输入ID和数量,并且我试图用相同的行ID汇总数量 ID数量 1234 1 1234 1 89762 89762 所以当我按下一个按钮时,ID1234将有2个数量,8976将有4个数量。 我厌倦了在system.linq中使用groupby函数,但我的列和行的单元格都是空的。有人有解决这个问题的办法吗?任何帮助都将不胜感激 下面是我的代码片段 我的主修课
1234 1 1234 1 89762 89762 所以当我按下一个按钮时,ID1234将有2个数量,8976将有4个数量。 我厌倦了在system.linq中使用groupby函数,但我的列和行的单元格都是空的。有人有解决这个问题的办法吗?任何帮助都将不胜感激 下面是我的代码片段 我的主修课
int id = 0;
int qty = 0;
List<QuantityPerID> qtyList = new List<QuantityPerID>();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (null != row.Cells[0].Value)
{
id = int.Parse(row.Cells[0].Value.ToString());
}
if (null != row.Cells[1].Value)
{
qty = int.Parse(row.Cells[1].Value.ToString());
}
var sales = qtyList.Select(s => s.ID).Distinct().Select(order => new QuantityPerID
{
ID = order,
Quantity= qtyList.Where(s => s.ID == order).Sum(s => s.Quantity),
}).ToList();
var source = new BindingSource();
source.DataSource = qtyList;
dataGridView1.DataSource = sales;
当前查询的结果是什么?标题ID和数量显示,而其余的数字变为空单元格。其他标题是什么?显然,不会显示,因为您只将ID和数量传递给gridview Source。我只得到了两个标题,即ID和数量。。但是你知道我怎么把数字传给gridview源代码吗?
class QuantityPerID
{
public int ID{ get; set; }
public int Quantity { get; set; }
}