1.11M Members

Transfering items from listview to datagridview

 
0
 

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
 
0
 

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

 
0
 

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.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article