Question: I used code for sum cells of `datagridview1` like below 

```
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    int sum = 0;
    for (i = 0; i < dataGridView1.Rows.Count - 1; i++)
    {
        if (dataGridView1.Rows[i].Cells[0].Value != null)
        {
            sum += Convert.ToInt32(dataGridView1.Rows[i].Cells[0].Value.ToString());
            label1.Text = Convert.ToString(sum);
        }
    }
}

```

but i want my `cell[0].value` sum is shown in `label1` is ok like below,but if i can use multiplied by any numbers in other cells like below image the answer should be = `60`
see below details how its come 

> 
> 5+4 = 9 \* 2 = 18 
> 
> 
> (18+2) \* 3 = 60
> 
> 
> 

![enter image description here](https://i.stack.imgur.com/15Huv.jpg)

Answer: 
Try this solution:

```
int sum = 0;
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
    //for 1st column
    if (dataGridView1.Rows[i].Cells[0].Value != null && !String.IsNullOrEmpty(dataGridView1.Rows[i].Cells[0].Value.ToString()))
        sum += Convert.ToInt32(dataGridView1.Rows[i].Cells[0].Value.ToString());

    //for 2nd column
    if (dataGridView1.Rows[i].Cells[1].Value != null && !String.IsNullOrEmpty(dataGridView1.Rows[i].Cells[1].Value.ToString()))
        sum *= Convert.ToInt32(dataGridView1.Rows[i].Cells[1].Value.ToString());

    //for 3rd column
    if (dataGridView1.Rows[i].Cells[2].Value != null && !String.IsNullOrEmpty(dataGridView1.Rows[i].Cells[2].Value.ToString()))
        sum *= Convert.ToInt32(dataGridView1.Rows[i].Cells[2].Value.ToString());
}

label1.Text = Convert.ToString(sum);

```

Assign `sum` to `label1` after all calculations. Its useless to do it in every iteration.
I think i have a solution for you, but it requires a Gridview, and not a datagridview so leave a comment if you will use it otherwise ill delete this.

```
foreach(GridViewRow row in gridCriteria.Rows)
    {
      foreach(TableCell cell in row.Cells)
      {
          if(cell.Text != "")
          {
                int indexOf = row.Cells.GetCellIndex(cell);

              if(indexOf == 0)
              {
                  Sum += Convert.ToInt32(cell.Text);
              }
              else
              {
                  Sum *= Convert.ToInt32(cell.Text);
              }
         }
      }

    }

```