Hi All,

Trying to add items from listview to datagridview, but keep coming up with this error:
"Specified argument was out of the range of valid values.
Parameter name: rowIndex"

I can add one row with no errors, but when I try to add two or more at the same time, this error occurs.

This is my code:

For i As Integer = 0 To form.lvStops.SelectedItems.Count - 1

        For Each item As ListViewItem In form.lvStops.SelectedItems
            Dim row As New DataGridViewRow
            frmAddLoad.Instance.DataGridView1.Rows.Add(row)
            With row
                .Cells(0).Value = DateTime.Now
                .Cells(1).Value = form.lvStops.SelectedItems(i).Text
                .Cells(2).Value = form.lvStops.SelectedItems(i).SubItems(1).Text
                .Cells(3).Value = form.lvStops.SelectedItems(i).SubItems(2).Text
                .Cells(4).Value = form.lvStops.SelectedItems(i).SubItems(3).Text
                .Cells(5).Value = form.lvStops.SelectedItems(i).SubItems(4).Text
                .Cells(6).Value = form.lvStops.SelectedItems(i).SubItems(5).Text
                .Cells(7).Value = form.lvStops.SelectedItems(i).SubItems(6).Text
                .Cells(8).Value = form.lvStops.SelectedItems(i).SubItems(7).Text
                .Cells(9).Value = form.lvStops.SelectedItems(i).SubItems(8).Text
                .Cells(10).Value = form.lvStops.SelectedItems(i).SubItems(9).Text
                .Cells(11).Value = form.lvStops.SelectedItems(i).SubItems(10).Text
            End With


        Next
    Next

    Any help would be appreciated!

    Zcast

Recommended Answers

All 2 Replies

Don't you need to add the row after setting the values?

Try this pattern, it's one of many that will work.

  With DataGridView1.Rows(DataGridView1.Rows.Add())
     .Cells(0).Value = "hi"
  End With

"DataGridView1.Rows.Add()" returns the index of the row that is just added. Doing it this way you add an empty row that is templated to accept the values that the cells expect.

Edit: This assumes that you have defined the columns of the DGV elsewhere.

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.