次の手順に従います。
1)データグリッド。
2)同様の項目(x、y、zなど)をループ内で識別し、それらを合計します。
int SumX=0;
int SumY=0;
int SumZ=0;
for (int i = 0; i < dataGridView1.Rows.Count; ++i)
{
if(Convert.ToString(dataGridView1.Rows[i].Cells[1].Value == "x")
sumX += Convert.ToDouble(dataGridView1.Rows[i].Cells[2].Value);
else if(Convert.ToString(dataGridView1.Rows[i].Cells[1].Value == "y")
sumY += Convert.ToDouble(dataGridView1.Rows[i].Cells[2].Value);
else if(Convert.ToString(dataGridView1.Rows[i].Cells[1].Value == "z")
sumZ += Convert.ToDouble(dataGridView1.Rows[i].Cells[2].Value);
}
ここにはexampleがあります。
LINQクエリを使用すると、それはかなり簡単です。
int SumX = dataGridView1.Rows.Cast<DataGridViewRow>()
.Where(r=> Convert.ToInt32(r.Cells["Item"].Value) == "x")
.Sum(t=> Convert.ToInt32(t.Cells["Count"].Value));
編集
本当にこの総和は、動的にしたいなら、あなたはここにthis.Basicallyような何かを行うことができ、同一の項目を(s)を追跡して、対応を合計する辞書ですカウント。
Dictionary<string, int> dic = new Dictionary<string, int>();
string item = null;
for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
{
item = dataGridView1.Rows[i].Cells[1].Value.ToString();
if (!dic.ContainsKey(item))
{
dic.Add(item, Convert.ToDouble(dataGridView1.Rows[i].Cells[2].Value);
}
else
{
dic[item] += Convert.ToDouble(dataGridView1.Rows[i].Cells[2].Value);
}
}
これで、辞書をループしてユニークなアイテム数を取得できます。
foreach (KeyValuePair<string, int> keyvalue in dic)
{
//get it here
}
希望します。
あなたは、C#ソリューションまたはSQL 1をお探しですか?それがSQLソリューションであれば、どのデータベースを使用していますか? –
http://stackoverflow.com/questions/18417333/how-we-add-numaric-value-of-multiple-cell-of-a-datagridview/18418027#18418027 – SK2185
@Senthilkumar、前にリンクについて少し説明してくださいそれを埋める。 –