我在这三个中有 7 列是只读字段,并且基于消耗品名称,我想对已发行数量字段求和。
我想根据消耗品名称汇总发行数量,并将总和与所需数量进行比较 。它们有很多消耗品名称。我的问题 How to calculate the Quantity Issued Column based on Consumable Name。我试过了,最后我得到了答案,这在逻辑上我无法解释。
我在这三个中有 7 列是只读字段,并且基于消耗品名称,我想对已发行数量字段求和。
我想根据消耗品名称汇总发行数量,并将总和与所需数量进行比较 。它们有很多消耗品名称。我的问题 How to calculate the Quantity Issued Column based on Consumable Name。我试过了,最后我得到了答案,这在逻辑上我无法解释。
1)创建一个字符串列表,其中包含“Consumable Name”列的所有行。填写姓名。
2)创建一个整数列表,该列表等于上述列表中的项目数。填写数量。
3)从(1)创建一个不同字符串的列表。
4)创建一个整数列表,等于(3)中的项目数。
5)进入每个不同的元素,并获得相关匹配的“消耗品名称”并添加数量。
6)如果您想比较或检查其他列,您可以遵循类似的步骤。
你可以参考下面的代码:
public void getQuantity()
{
List<string> consumbleNames = new List<string>();
List<int> quantity = new List<int>();
//Cells[5] refer to 6th column : Quantity issued: you can use column name too
for (int i = 0; i < yourDataGridView.Rows.Count; i++)
{
consumbleNames.Add(yourDataGridView.Rows[i].Cells[0].Value.ToString());
quantity.Add((int)Convert.ToInt32(yourDataGridView.Rows[i].Cells[5].Value.ToString()));
}
List<string> distinctNames = new List<string>(consumbleNames.Distinct());
List<int> quantityRelated = new List<int>(distinctNames.Count);
for (int i = 0; i < distinctNames.Count; i++)
{
quantityRelated.Add(new int());
for (int j = 0; j < consumbleNames.Count; j++)
{
if (consumbleNames[j].Equals(distinctNames[i]))
{
quantityRelated[i] += quantity[j];
}
}
}
foreach (int t in quantityRelated)
{
/* quantity corresponds to the each distinct item */
}
}