0
votes

When I execute my application, then the following error occurs:

System.ArgumentOutOfRangeException was unhandled by user code. Specified argument was out of the range of valid values. Parameter name: index

The error occurs on the line below which is commented:

private void helper_GeneralSummary(GridViewRow row)
{
 double HoldingDays = Convert.ToDouble(row.Cells[6].Text);
 double AbsReturn = Convert.ToDouble(row.Cells[7].Text); 
 double AnnReturn = (AbsReturn / HoldingDays) * 365;
 row.Cells[8].Text = AnnReturn.ToString("#,0.00"); //Error occured
}
1
You know that the first cell is row.Cells[0]? - Tim Schmelter
Try to debug and inspect the objects values, it will make it easier to get answer - Tamir

1 Answers

1
votes

An ArgumentOutOfRangeException means that you have provided a value, that is too big or too small for what you are doing. In your particular example you're trying to access the contents of an array:

row.Cells[8].Text = AnnReturn.ToString("#,0.00");

You can ignore most of that line, the error is occuring just in this section:

row.Cells[8]

It's telling you that the Cells array, does not have 9 columns. Bear in mind this is 0-indexed, so a index of 8 means access column 9. Conversely to access the first column you would use row.Cells[0].

You've therefore made a bad assumption somewhere and need to check what data you've got out the system first. If you stick a breakpoint on that line you should be able to dig into row.Cells as see exactly what columns you've got to work with.