如何将datagridview中的单元格值与不同的列值相加

时间:2017-04-15 13:20:22

标签: c# datagridview

我尝试在datagridview c#application

中对列值求和

enter image description here

我写了这段代码来汇总列值..

private void SetAmount()
{
  decimal ldQuantity =0.0m, ldTotalWeight=0.0m, ldAmount = 0.0m;
  for (int i = 0; i < dataGridView1.Rows.Count; i++)
  {
    if (dataGridView1[2, i].Value.ToString() == "916")
    {
      ldQuantity = ldQuantity + Convert.ToDecimal(dataGridView1[4, i].Value);
      lbl916.Text = ldQuantity.ToString();
    }
    else if (dataGridView1[2, i].Value.ToString() == "22ct")
    {
      ldQuantity = ldQuantity + Convert.ToDecimal(dataGridView1[4, i].Value);
      lbl22.Text = ldQuantity.ToString();
    }
    else if (dataGridView1[2, i].Value.ToString() == "90")
    {
      ldQuantity = ldQuantity + Convert.ToDecimal(dataGridView1[4, i].Value);
      lbl90.Text = ldQuantity.ToString();
    }
    else if (dataGridView1[2, i].Value.ToString() == "Silver")
    {
      ldQuantity = ldQuantity + Convert.ToDecimal(dataGridView1[4, i].Value);
      lblSilver.Text = ldQuantity.ToString();
    }
    ldTotalWeight= ldTotalWeight+ Convert.ToDecimal(dataGridView1[6, i].Value);
    lblTotalWeight.Text = ldTotalWeight.ToString();
    if (dataGridView1[8, i].Value != null)
    {
      ldAmount = ldAmount + Convert.ToDecimal(dataGridView1[8, i].Value);
      lblTotal.Text = ldAmount.ToString();
    }
  }
  //dataGridView1.Columns[4] = ldMultiply.ToString();
}

但是根据我的代码,它对于相同的单元格值一次又一次地对单元格值求和......

但我想要这样

JewelID       JewelName  Purity    Quantity
J0001         Chain      916         4
J0003         Kolusu     Silver      10
Label916Quantity.Text  = 4
LabelSilverQuantity.Text = 10

请改变我的代码......

0 个答案:

没有答案