Home > OS >  How to cycle continuously through DataGridView rows?
How to cycle continuously through DataGridView rows?

Time:09-16

This might seem like a repeated question, but I am looking for a specific function. I have looked through similar questions on StackOverflow and Google and tried using many different code examples, but up to now, without success?

What I Am Doing:

  1. At runtime, i.e. Form1_Load, I call a function to display File Info in a DataGridView for all the files in "MyFolder".
  2. I use Next/Previous buttons to cycle through DGV rows.

My Code:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    'Call Function To Display File Info From MyFolder:
    DataGridView1.DataSource = Fileinfo_To_DataTable("C:\Users\"   username   "\Documents\MyApp\MyFolder")
End Sub

'Next Button:
Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click

    If DataGridView1.SelectedRows(0).Index < DataGridView1.RowCount - 1 Then
        MyDesiredIndex = DataGridView1.SelectedRows(0).Index   1
    Else
        MyDesiredIndex = 0
    End If

    DataGridView1.ClearSelection()
    DataGridView1.CurrentCell = DataGridView1.Rows(MyDesiredIndex).Cells(0)
    DataGridView1.Rows(MyDesiredIndex).Selected = True
End Sub

'Previous Button:  
Private Sub btnPrev_Click(sender As Object, e As EventArgs) Handles btnPrev.Click
    If DataGridView1.CurrentCell.RowIndex >= 0 And DataGridView1.CurrentCell.RowIndex <= DataGridView1.Rows.Count - 1 Then
        For Each row As DataGridViewRow In DataGridView1.Rows
            If Not row.IsNewRow Or vbNull Then
                MyDesiredIndex = DataGridView1.SelectedRows(0).Index - 1
            End If
        Next
    End If

    DataGridView1.ClearSelection()
    DataGridView1.CurrentCell = DataGridView1.Rows(MyDesiredIndex).Cells(0)
    DataGridView1.Rows(MyDesiredIndex).Selected = True
End Sub

The Problem:
The Next button cycles in a "continuous loop" through all DGV rows without exceptions. By "continuous loop" I mean that my program cycles through all rows without stopping, either at the first row (0) or at the last row (i.e. cycling occurs as long as I continue to press the Next button).

The Previous button only works if I first use the Next button to change the selected row (i.e. First > Last). Then, hitting the Previous button changes the selected row returning to the first row (i.e. Last > First). But, when the program reaches the first row it throws an exception as follows:

"System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index'"

  1. Resolve the Out of Range exception.
  2. Resolve the cycling/looping through all rows issue?

What I Have Tried:
Besides my attempt above (and many others), I found the following code on StackOverflow which addresses the same issue, but which also stops at the first row without cycling through all rows:

Moving to previous row in datagridview
BindingSource DataGridView MoveNext MovePrevious

  • Related