1)「消耗品名」列のすべての行を含む文字列のリストを作成します。名前を入力します。
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 */
}
}