The first time round these loops, what is the value of
i
?
Simple:
for (int i = 0; i ...
So teh first time round the loop, if this test works:
if (dataGridView5.Rows[1].Cells[i].Value != "0")
Then
this access will be guaranteed to fail:
... +int.Parse( dataGridView5.Rows[1].Cells[i-1].Value.ToString());
as arrays cannot have negative indexes...