Hi. I have a datagridview on a form that displays values generated in a for loop when a user presses a button. Here's the code.

Dim i as Integer
Dim Period as double
Dim rate as double
Dim interest as Double
Dim amount as double
Dim payments as Double

'The values for period, rate and amount have been inputed by the user. Payments have already been calculated

For i = 1 to Period

'calculate the interest
interest = (amount * rate) / 12

'calculate the amount
amount = (amount - payment) + interest

'output the data in the data grid view

dgvInterest.Rows.Add()
dgvInterest.Item("Interest",i-1).Value = FormatCurrency(interest)
dgvInterest.Item("Amount",i-1).Value = FormatCurrency(amount)

Next

This worked alright but now i want the loop to only display the data from a specific period and not the beginning.

For i = 1 to Period

'calculate the interest
interest = (amount * rate)/12

'calculate the amount
amount = (amount - payment) + interest

'output only the desired data in the data grid view

If i >10 Then
dgvInterest.Rows.Add()
dgvInterest.Item("Interest",i-1).Value = FormatCurrency(interest)
dgvInterest.Item("Amount",i-1).Value = FormatCurrency(amount)

End If

Next

When I do this i get an error saying that i is out of index. I have tried while loop, do loop to output the data but they all give me the same error.
I only want the data from when the variable i >10 to be displayed on the datagridview.
If anyone can please show me how to effectively do this.

Dani AI

Generated

The "Index out of range" happens because the loop counter i is being used as a DataGridView row index while rows are only being added once i > 10. i-1 assumes a row already exists for every iteration from 1, so when the code skips the first ten iterations the grid has far fewer rows than i-1 expects. This is the root cause behind the exception (and what was pointing out when recommending not to use i as the grid row index). Also check the Period type as noted and confirm the loop uses an integer iteration count.

Two safe approaches that avoid the index mismatch:

  • Add a row and set its cells in one call (no manual index math required). This uses the overload that takes cell values:
' inside the loop, after computing interest and amount:
If i > 10 Then
  dgvInterest.Rows.Add(FormatCurrency(interest), FormatCurrency(amount))
End If
  • Maintain a separate counter for visible rows and use it when assigning cells, so the grid index is always correct:
Dim visibleRow As Integer = 0
For i = 1 To CInt(Math.Floor(Period))
  ' compute interest/amount here
  If i > 10 Then
    dgvInterest.Rows.Add()
    dgvInterest.Rows(visibleRow).Cells("Interest").Value = interest
    dgvInterest.Rows(visibleRow).Cells("Amount").Value = amount
    visibleRow += 1
  End If
Next

Practical notes: perform the numeric calculations for every period (so the amortisation state remains correct) but only add rows when ready to display. Prefer storing numeric values in the grid and set the column format to currency (for example, DefaultCellStyle.Format = "C2") rather than inserting formatted strings — that keeps sorting and calculations numeric. Also use an integer loop bound (cast Period to an integer) and, as suggested, inspect Rows.Count/Cells.Count while debugging to confirm expected row/column counts.

For large numbers of rows, populate a DataTable and bind it to the grid for better performance instead of adding rows one-by-one.

Recommended Answers

All 4 Replies

I'm not sure if this is true, but maybe since the variable Period is a double, it is rounding up to the next whole number then testing. So in the For loop, try: For i = 1 to Period - 1 or even For i = 1 to Period.ToInt32 Again, I'm not sure, but it is worth a try!

It looks as though at the time your are trying to retrieve the data, your GridView has no Rows or Cells. Check the Rows.Count and Rows(0).Cells.Count properties to check which one is failing.

thanks
The initial loop is working, what i need help with is the if statement

If i > 10 Then
//display output
End IF

This is where the problem is.. When i change the if statement to a while or do loop I still get the error. " i is out of index" I need the for loop to display ONLY the values generated after i has reached a value greater than ten to the period. I dont know how to nest a second loop in to do this.

Your error must be appearing in one of these lines:

dgvInterest.Rows.Add()
dgvInterest.Item("Interest",i-1).Value = FormatCurrency(interest)
dgvInterest.Item("Amount",i-1).Value = FormatCurrency(amount)

You can't use "i" as the rowindex in this case, "i" is the counter for your loop and has nothing to do with the datagridview you want to fill.
If you want to populate the cells in the row you just added, make sure to use the rowindex that it returned:

Dim rowindex As Integer = dgvInterest.Rows.Add()
dgvInterest.Item("Interest", rowindex).Value = FormatCurrency(interest)
dgvInterest.Item("Amount",rowindex).Value = FormatCurrency(amount)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.