15,882,464 members
See more:
hello,
i want two multiply two column's value one by one and then print the sum of these multiplied answers in a text box. i have written a code but it gives out of range error.the code is this:

d = 0

For r = 0 To a - 1
c = (DataGridView1.Item(j + 3, r + 1).value * DataGridView1.Item(j + 2, r + 1).value)
d = d + c
Next r
TextBox2.Text = Str(d)

here "r" are rows and "a" is total no.of rows given by user."j" are columns

kindly reply me as soon as possible i need this urgently.
Posted

## Solution 1

Hi

I Think This code is helpful you
VB
```Double val1 = Convert.ToDouble(dataGridView1.Rows[e.RowIndex].Cells[4].Value);
Double val2 = Convert.ToDouble(dataGridView1.Rows[e.RowIndex].Cells[6].Value);
dataGridView1.Rows[e.RowIndex].Cells[7].Value = val1 * val2;
//val3 = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[8].Value);
//tb_grossamt.Text = (val1 * val2).ToString();
Double valgrd = (val1 * val2);
Double valtax = Convert.ToDouble(tb_tax.Text);
Double taxtot = (valgrd * valtax / 100);
Double ftot = (valgrd + taxtot);```

v3
VJ Reddy 7-Jun-12 2:33am
Good suggestion. 5!
Member 10030245 21-May-13 15:26pm
on which event

## Solution 2

The reason for getting the out of range error may be because the `DataGridView ``Rows `collection is zero based whereas in the following code
VB
`c = (DataGridView1.Item(j + 3, r + 1).value * DataGridView1.Item(j + 2, r + 1).value)`

`r+1` for `upper bound value` of the loop makes `Row index` as `a` which causes the above error.

So replace `r+1` with `r` in the above code further it is better to convert the value to `Double `for multiplication as already suggested in `Solution 1`, as shown below:
VB
`c = Convert.ToDouble(DataGridView1.Item(j + 3, r + 1).value) * Convert.ToDouble(DataGridView1.Item(j + 2, r + 1).value)`

Syed Hamza 7-Dec-19 4:07am
how can i do this in datagrid.