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.

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.